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

Build list of all values in group and apply to entire group

I have a dataframe like the below:

x <- tibble(id = c(1,1,1,2,2,2),
            val = c(1,3,5,7,9,11)
            )

I would like to group_by each id, then apply a list/vector of all the vals in each group to every member of that group. The result would be a dataframe like that below.

x_out <- tibble(id = c(1,1,1,2,2,2),
                    val = c(1,3,5,7,9,11),
                    group_vals = list(c(1,3,5),c(1,3,5),c(1,3,5),
                                c(7,9,11),c(7,9,11),c(7,9,11))
               )

How can I do this, since functions like summarize only return a single value for the entire group?

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

>Solution :

out <- x %>%
  group_by(id) %>%
  mutate(group_vals = list(val)) %>%
  ungroup()
out
# # A tibble: 6 x 3
#      id   val group_vals
#   <dbl> <dbl> <list>    
# 1     1     1 <dbl [3]> 
# 2     1     3 <dbl [3]> 
# 3     1     5 <dbl [3]> 
# 4     2     7 <dbl [3]> 
# 5     2     9 <dbl [3]> 
# 6     2    11 <dbl [3]> 

We can see what the group_vals looks like with:

str(out)
# tibble [6 x 3] (S3: tbl_df/tbl/data.frame)
#  $ id        : num [1:6] 1 1 1 2 2 2
#  $ val       : num [1:6] 1 3 5 7 9 11
#  $ group_vals:List of 6
#   ..$ : num [1:3] 1 3 5
#   ..$ : num [1:3] 1 3 5
#   ..$ : num [1:3] 1 3 5
#   ..$ : num [1:3] 7 9 11
#   ..$ : num [1:3] 7 9 11
#   ..$ : num [1:3] 7 9 11

Verification, using your x_out definition above:

identical(out, tibble(id = c(1,1,1,2,2,2),
                    val = c(1,3,5,7,9,11),
                    group_vals = list(c(1,3,5),c(1,3,5),c(1,3,5),
                                c(7,9,11),c(7,9,11),c(7,9,11))
               ))
# [1] TRUE
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