I see that in dplyr the function cur_data() has been deprecated in favor of pick(). However, I’m confused about how to use pick() when trying to add rows to each group in a grouped dataframe. I’m cleaning some funky data and need to insert rows into each group that propagate the grouping variable’s value while inserting a specific value into an accompanying column.
Here’s example code that uses cur_data() to produce my desired output:
df <- tibble::tribble(
~id, ~val,
"A", 95,
"A", 20,
"A", 45,
"B", 10,
"B", 50,
"C", 80
)
df_new_rows <- df |>
dplyr::group_by(id) |>
dplyr::reframe(tibble::add_row(dplyr::cur_data(), val = 100)) |>
dplyr::ungroup()
Here’s the desired output – the rows with 100 in the val column have been inserted:
# A tibble: 9 × 2
id val
<chr> <dbl>
1 A 95
2 A 20
3 A 45
4 A 100
5 B 10
6 B 50
7 B 100
8 C 80
9 C 100
How can I use pick() or another function to do this? For development reasons I’d like to use either base R or tidyverse functions, but if there is no way to do that I’m open to other suggestions. Thanks in advance for any help!
>Solution :
You can use the group_modify() to applies a function to each group in a grouped tibble, and returns a grouped tibble.
df <- tibble::tibble(
id = c("A", "A", "A", "B", "B", "C"),
val = c(95, 20, 45, 10, 50, 80)
)
add_row_func <- function(df) {
tibble::add_row(df, id = df$id[1], val = 100)
}
df_new_rows <- df |>
dplyr::group_by(id) |>
dplyr::group_modify(add_row_func) |>
dplyr::ungroup()
print(df_new_rows)