I have a data frame:
df <- data.frame(id = c("a","a","a","b","b","b"), survey = rep("1a",6), q1 = c(NA,NA,"y","n",NA,NA),q2 = c("y",NA,NA,NA,"y",NA), q3 = c(NA,"n",NA,NA,NA,"y"))
These are survey data which I need to collapse to one for each id and survey. I can get close by:
df %>% group_by(id, survey) %>% summarize_all(toString)
Source: local data frame [2 x 5]
Groups: id [?]
id survey q1 q2 q3
<fctr> <fctr> <chr> <chr> <chr>
1 a 1a NA, NA, y y, NA, NA NA, n, NA
2 b 1a n, NA, NA NA, y, NA NA, NA, y
What I really need is:
id survey q1 q2 q3
1 a 1a y y n
2 b 1a n y y
The real data frame is fairly large (1.2 million records).
Fundamentally different from suppress NAs in paste(). Answer to my question not found there.