1

I have perused the existing posts on dcast and the official dcast vignette. I may not have framed my searches correctly, but I haven't found an example quite like mine. Take a look at my toy data on excel (which I can read into R). It's already in melted form. As you can see, the student~day columns have multiple duplicate entries because a student can wear multiple articles of clothing on the same day. I want to recast as wide without losing duplicate rows (see arnav's first two entries)--

Student Day Clothes
Arnav Mon red shirt
Arnav Mon blue pants
Beth Mon dress
Chuo Mon dress
Beth Tues green shirt
Beth Tues blue pants
Chuo Tues dress

For each student on each day, I want to combine the clothes' column like this:

Student Day Clothes
Arnav Mon red shirt, blue pants
Beth Mon dress
Chuo Mon dress
Beth Tues green shirt, blue pants
Chuo Tues dress

Thank you so much!

1 Answers1

0

The key is using the unwrap_cols function from the unheadr package. I thought there might be something in the tidyr package (opposite of the separate_rows function but couldn't find anything).

library(unheadr)
library(tidyverse)

clothes <- tibble(
  Student = c('Arnav', 'Arnav', 'Beth', 'Chuo', 'Beth', 'Beth', 'Chuo'), 
  Day = c('Mon', 'Mon', 'Mon', 'Mon', 'Tues', 'Tues', 'Tues'),
  Clothes = c('red shirt', 'blue pants', 'dress', 'dress', 'green shirt', 'blue pants', 'dress')
)

clothes %>% 
  unite(StudentDay, Student:Day) %>%
  unwrap_cols(groupingVar = StudentDay, separator = ', ') %>%
  separate(StudentDay, into = c('Student', 'Day'))
Harrison Jones
  • 2,256
  • 5
  • 27
  • 34
  • unfortunately I got these errors + warnings (I am googling as we speak): Error in unite(., StudentDay, Student:Day) : lazy-load database '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/tidyr/R/tidyr.rdb' is corrupt In addition: Warning messages: 1: In unite(., StudentDay, Student:Day) : restarting interrupted promise evaluation 2: In unite(., StudentDay, Student:Day) : internal error -3 in R_decompress1 – dunkindonts Sep 27 '21 at 15:35
  • That to me looks like you have an error in your loading of the `tidyr` package. You might want to reinstall that one (or `tidyverse` in general). I'm not able to reproduce that error message. – Harrison Jones Sep 27 '21 at 15:48
  • Yeah! Ultimately, I had to restart R and re-select tidyr and tidyverse from the packages window – dunkindonts Sep 28 '21 at 16:33
  • So did it work? – Harrison Jones Sep 28 '21 at 19:30
  • yes it did, but after doing this and re-loading tidyr, my pipe character (%>%) no longer works. I am running the same lines that used to work before I implemented your solution. They used to run just fine, but now I'm getting the error, "Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables" I've looked at threads with this error, but they had something wrong with their data or the functions they wanted to pipe to. However, it used to work (ex: df %>% subset(.,.$Day=="Monday"). If I restart R and don't load tidyverse, the pipe works again – dunkindonts Sep 29 '21 at 18:20
  • If your original question is answered then please accept the answer I posted. This latest comment sounds like a different problem that you're experiencing. If that's the case then feel free to post a new question so it can be solved separately. – Harrison Jones Sep 30 '21 at 14:00
  • No problem, but I wrote the comment because most users will have both libraries loaded and may experience similar problems – dunkindonts Oct 03 '21 at 19:45