0

I have the following data:

  df = data.frame(
  id("anton", "anton", "charly", "charly", "klaus", "klaus"),
  fruits=c("apple", "cherry", "pear", "pear", "apple", "pear"),
  number=c(1,4,1,2,3,5))

      id       fruits   number
  1   anton    apple    1
  2   anton    cherry   4
  3   charly   pear     1
  4   charly   pear     2
  5   klaus    apple    3
  6   klaus    pear     5

desired outcome:

      id       fruits             number
  1   anton    apple, cherry      1, 4
  2   charly   pear, pear         1, 2
  3   klaus    apple, pear        3, 5
 

it works with

library(dplyr)
df.wide <- df %>%
group_by(id) %>%
summarise_all(funs(toString(na.omit(.))))

but I get the warning

"funs() is deprecated as of dplyr 0.8.0. Please use a list of either functions or lambdas:

Simple named list: list(mean = mean, median = median)

Auto named with tibble::lst(): tibble::lst(mean, median)

Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))".

How could I reproduce it? 'list' and then 'unnest'? (tried it, but cannot wrap my head around it how to unnest all columns)

2 Answers2

1

Note that also summarise_all is depreciated. Instead you can use across together with a purrr style lambda function:

df = data.frame(
  id = c("anton", "anton", "charly", "charly", "klaus", "klaus"),
  fruits=c("apple", "cherry", "pear", "pear", "apple", "pear"),
  number=c(1,4,1,2,3,5))

library(dplyr)

df.wide <- df %>%
  group_by(id) %>%
  summarise_all(funs(toString(na.omit(.))))
df.wide
#> # A tibble: 3 x 3
#>   id     fruits        number
#>   <chr>  <chr>         <chr> 
#> 1 anton  apple, cherry 1, 4  
#> 2 charly pear, pear    1, 2  
#> 3 klaus  apple, pear   3, 5

df_new <- df %>% 
  group_by(id) %>% 
  summarise(across(everything(), ~toString(na.omit(.))))
df_new
#> # A tibble: 3 x 3
#>   id     fruits        number
#>   <chr>  <chr>         <chr> 
#> 1 anton  apple, cherry 1, 4  
#> 2 charly pear, pear    1, 2  
#> 3 klaus  apple, pear   3, 5

Created on 2020-09-25 by the reprex package (v0.3.0)

starja
  • 9,887
  • 1
  • 13
  • 28
1

Try using across() in combination with everything()

df %>%
  group_by(id) %>%
  summarise(fruits = paste(fruits, collapse = ", "),
            number = paste(number, collapse = ", "))

df %>%
  group_by(id) %>%
  summarise(across(everything(), ~paste(., collapse = ", ")))

which yields

  id     fruits        number
  <chr>  <chr>         <chr> 
1 anton  apple, cherry 1, 4  
2 charly pear, pear    1, 2  
3 klaus  apple, pear   3, 5  

for examples on how to use these new functions, see: https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/

L Smeets
  • 888
  • 4
  • 17