How to Add Row to Data Frame Using dplyr


Often you may want to use the dplyr package in R to add new rows to an existing data frame.

Fortunately this is easy to do by using the add_row() function, which is designed to perform this exact task.

The add_row() function uses the following basic syntax:

add_row(.data, .before=NULL, .after=NULL)

where:

  • .data: The name of the existing data frame to add rows to
  • .before: The row index to add new row before
  • .after: The row index to add new row after

Note that if you don’t specify any values for the .before or .after arguments then the new row will simply be added to the end of the data frame.

The following examples show how to use the add_row() function from the dplyr package to add a new row to an existing data frame, using the following data frame as an example:

#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: Add Row to End of Data Frame Using dplyr

Suppose that we would like to add a new row with specific values to the end of the existing data frame.

We can use the following syntax to do so:

library(dplyr)

#add new row at end of data frame
df %>% add_row(team='A', points=90, assists=40, rebounds=20)

  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
9    A     90      40       20

Notice that the new row has been added to the end of the existing data frame.

Example 2: Add Row Before Specific Row of Data Frame Using dplyr

Suppose that we would like to add a new row with specific values before row index position 5 of the existing data frame.

We can use the following syntax to do so:

library(dplyr)

#add new row before row index position 5 of data frame
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 the new row has been added before row index 5 of the existing data frame, just as we specified by using the .before argument.

Example 3: Add Row After Specific Row of Data Frame Using dplyr

Suppose that we would like to add a new row with specific values after row index position 5 of the existing data frame.

We can use the following syntax to do so:

library(dplyr)

#add new row after row index position 5 of data frame
df %>% add_row(team='A', points=90, assists=40, rebounds=20, .after=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    B     95      34       30
6    A     90      40       20
7    B     74      45       36
8    B     78      28       30
9    B     93      31       29

Notice that the new row has been added after row index 5 of the existing data frame, just as we specified by using the .after argument.

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

Note #2: You can find the complete documentation for the add_row() function from the dplyr package here.

Additional Resources

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

How to Insert Row into 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 *