Repeating code to add blank rows in dataframe

I am trying to find a way to replicate the following code 4 times:

df3_1<- df3_1 %>% add_row(.before
= 2)

I tried the 'rep' function but it didn't work out. Is there any way to repeat this code 4 times so I can add multiple blank rows exactly in the manner described above (i.e. with respect to a specific row number.

Thanks!

You can supply vectors to add multiple rows.

library(tibble)

df <- tibble(col1 = c("A", "B"), col2 = c(3, 4))

add_row(df, col1 = rep(NA, 4), col2 = rep(NA, 4), .before = 2)
#> # A tibble: 6 x 2
#>   col1   col2
#>   <chr> <dbl>
#> 1 A         3
#> 2 <NA>     NA
#> 3 <NA>     NA
#> 4 <NA>     NA
#> 5 <NA>     NA
#> 6 B         4

Created on 2020-07-01 by the reprex package (v0.3.0)

Just out of curiosity, why do you want to do something like this? There might be a better way of achieving your desired result.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.