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

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

reshape(dat, idvar=”ID”, direction=”long”, varying=list(Start=c(2,5,8), End=c(3,6,9), Value=c(4,7,10)), v.names = c(“DateRangeStart”, “DateRangeEnd”, “Value”) ) #————- ID time DateRangeStart DateRangeEnd Value 1.1 1 1 1/1/90 3/1/90 4.4 1.2 1 2 4/5/91 6/7/91 6.2 1.3 1 3 5/5/95 6/6/96 3.3 (Added the v.names per Josh’s suggestion.)