How can I spread repeated measures of multiple variables into wide format?

Edit: I’m updating this answer since pivot_wider has been around for a while now and addresses the issue in this question and comments. You can now do pivot_wider( dat, id_cols=”Person”, names_from = ‘Time’, values_from = c(‘Score1’, ‘Score2’, ‘Score3’), names_glue=”{Time}.{.value}” ) to get the desired result. The original answer was dat %>% gather(temp, score, starts_with(“Score”)) %>% … Read more

Comparing gather (tidyr) to melt (reshape2)

Your gather line should look like: dat %>% gather(variable, date, -teacher, -pd) This says “Gather all variables except teacher and pd, calling the new key column ‘variable’ and the new value column ‘date’.” As an explanation, note the following from the help(gather) page: …: Specification of columns to gather. Use bare variable names. Select all … Read more

dplyr summarise: Equivalent of “.drop=FALSE” to keep groups with zero length in output

The issue is still open, but in the meantime, especially since your data are already factored, you can use complete from “tidyr” to get what you might be looking for: library(tidyr) df %>% group_by(b) %>% summarise(count_a=length(a)) %>% complete(b) # Source: local data frame [3 x 2] # # b count_a # (fctr) (int) # 1 … Read more

Gather multiple sets of columns

This approach seems pretty natural to me: df %>% gather(key, value, -id, -time) %>% extract(key, c(“question”, “loop_number”), “(Q.\\..)\\.(.)”) %>% spread(question, value) First gather all question columns, use extract() to separate into question and loop_number, then spread() question back into the columns. #> id time loop_number Q3.2 Q3.3 #> 1 1 2009-01-01 1 0.142259203 -0.35842736 #> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)