This is an example of a problem which I've solved, but not to my liking. I have a dataframe, 5 columns by 4884 observations, and I am trying to use tidyr::nest
and purrr::map
to build a nested data frame for use in a visualization. Here is the solution I tried to use:
rx_info1 <- rx_post %>%
nest(-mem_id, -`Reporting Name`) %>%
mutate(refill_df = map(data, function(df){
is_refill <- df %>%
group_by(group) %>%
mutate(tally = n(), # for testing
is_refill = n() > 1) %>%
select(group, is_refill) %>%
distinct() %>%
filter(!is.na(group))
left_join(df, is_refill, by = 'group')
}))
In that solution, each of the nested dataframes in the refill_df
column of rx_info1
has tally == 4884
, which is the length of the original data frame, and is_refill == F
. However, this solution works:
rx_info2 <- rx_post %>%
nest(-mem_id, -`Reporting Name`)
temp <- map(rx_info2$data, function(df){
is_refill <- df %>%
group_by(group) %>%
mutate(tally = n(), # for testing
is_refill = n() > 1) %>%
select(group, is_refill) %>%
distinct() %>%
filter(!is.na(group))
left_join(df, is_refill, by = 'group')
})
rx_info2$refill_df <- temp
In this case, tally
is indeed the tally of the "group" number of each individual data
data frame, and is_refill
does indeed give me the correct boolean value.
For some reason (which hopefully some of you can relate to), I would like for all of this to exist within a single pipe chain. Any help with this is much apprecaited.