r - Gather groups of columns -
this question has answer here:
i have data online survey respondents go through series of 5 questions. data working @ looks this:
p1 p2 p3 p4 p5 p1 p2 p3 p4 p5 p1 p2 p3 p4 p5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 6 7 8 9 10 6 7 8 9 10
the desired output is:
p1 p2 p3 p4 p5 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
i have been trying solve problem library tidyr, can't understand how apply it. advise on helpful.
one of solutions can be:
require(foreach) require(dplyr) require(magrittr) foreach( unique_name = unique(colnames(df)), .combine = 'bind_cols' ) %do% { df[grep(unique_name), colnames(df)] %>% unlist() %>% as.data.frame() %>% set_colnames(., unique_names) }
Comments
Post a Comment