HI suppose I have the following dataframe
df <- data.frame(group = rep(c("group_1","group_2","group_3", "group_4", "group_5", "group_6"), each=3),
X = paste(letters[1:18]),
Y = c(1:18))
df
group X Y
1 group_1 a 1
2 group_1 b 2
3 group_1 c 3
4 group_2 d 4
5 group_2 e 5
6 group_2 f 6
7 group_3 g 7
8 group_3 h 8
9 group_3 i 9
10 group_4 j 10
11 group_4 k 11
12 group_4 l 12
13 group_5 m 13
14 group_5 n 14
15 group_5 o 15
16 group_6 p 16
17 group_6 q 17
18 group_6 r 18
What I want is to insert 5 rows with values of -10 in the Y columns but retain the group id
I tried doing this based on a similar cross post but cannot seem to retain group id and this only seem to work to insert 1 row.
> df %>%
+ group_split(group) %>%
+ map_dfr(~ .x %>%
+ add_row(Y = -10, .after = 0))
# A tibble: 24 x 3
group X Y
<chr> <chr> <dbl>
1 NA NA -10
2 group_1 a 1
3 group_1 b 2
4 group_1 c 3
5 NA NA -10
6 group_2 d 4
7 group_2 e 5
8 group_2 f 6
9 NA NA -10
the example above fails because it only inserts 1 row moreover the group id is loss, ideally for example, there should be 5 rows with group_1 inserted with the Y values of -10.
is this possible? thanks in advance!