How to Insert Row into Data Frame in R


Often you may want to insert a row into a data frame in R in a specific index location.

While it’s easy to append rows to data frames in R, inserting rows turns out to be a bit more complex.

Fortunately there are two ways to do so:

Method 1: Insert Row Into Data Frame Using Base R

#insert new row into data frame
df <- rbind(df[1:row_num,], new_row, df[-(1:row_num),])

#reset row names of data frame
rownames(df) <- 1:nrow(df)

This particular example inserts a new row with the values specified in the new_row vector at the integer location specified by row_num.

Method 2: Insert Row Into Data Frame Using dplyr

library(dplyr)

df %>% add_row(team='A', points=90, assists=40, rebounds=20, .before=5)

This particular example uses the add_row() function from the dplyr package to insert a new row before row 5 in the data frame.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Example 1: Insert Row Into Data Frame Using Base R

Suppose that we would like to insert a new row with specific values into the existing data frame before the row position 4.

We can use the following syntax to do so:

#define new row to insert
new_row <- c('A', 90, 40, 20)

#specify row number to insert new row after
row_num <- 4

#insert new row into 
df <- rbind(df[1:row_num,], new_row, df[-(1:row_num),])

#reset row names of data frame
rownames(df) <- 1:nrow(df)

#view updated data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    A     90      40       20
6    B     95      34       30
7    B     74      45       36
8    B     78      28       30
9    B     93      31       29

Notice that a new row has been inserted into the data frame after row position 4, just as we specified using the row_num variable.

Feel free to change the value of the row_num variable to insert the new row into a different row index position.

Example 2: Insert Row Into Data Frame Using dplyr

Suppose that we would like to insert a new row with specific values into the existing data frame before the row position 4 using the add_row() function from the dplyr package.

We can use the following syntax to do so:

library(dplyr)

#add new row before row index position 5
df %>% add_row(team='A', points=90, assists=40, rebounds=20, .before=5)

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    A     90      40       20
6    B     95      34       30
7    B     74      45       36
8    B     78      28       30
9    B     93      31       29

Notice that a new row has been inserted into the data frame before row position 5, just as we specified using the .before argument of the add_row() function.

Feel free to change the value of the .before argument to insert the new row into a different row index position.

Note: The new row that you insert must contain the same number of values as the number of existing columns in the data frame.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert a List to a Data Frame in R
How to Append Values to List in R
How to Convert Data Frame Column to List in R
How to Count Number of Elements in List in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *