Add new row to dataframe, at specific row-index, not appended?

Viewed 432366

The following code combines a vector with a dataframe:

newrow = c(1:4)
existingDF = rbind(existingDF,newrow)

However this code always inserts the new row at the end of the dataframe.

How can I insert the row at a specified point within the dataframe? For example, lets say the dataframe has 20 rows, how can I insert the new row between rows 10 and 11?

6 Answers

The .before argument in dplyr::add_row can be used to specify the row.

dplyr::add_row(
  cars,
  speed = 0,
  dist = 0,
  .before = 3
)
#>    speed dist
#> 1      4    2
#> 2      4   10
#> 3      0    0
#> 4      7    4
#> 5      7   22
#> 6      8   16
#> ...

Insert blank row after five row in data frame and use this library package.

library(berryFunctions)

df <- insertRows(df, 5 , new = "")
Related