Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace `cur_data()` with `pick()` when adding a row to each group in a dataframe

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# 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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading