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

Reshape three column data frame to matrix (“long” to “wide” format) [duplicate]

There are many ways to do this. This answer starts with what is quickly becoming the standard method, but also includes older methods and various other methods from answers to similar questions scattered around this site. tmp <- data.frame(x=gl(2,3, labels=letters[24:25]), y=gl(3,1,6, labels=letters[1:3]), z=c(1,2,3,3,3,2)) Using the tidyverse: The new cool new way to do this is … Read more

Split delimited strings in a column and insert as new rows [duplicate]

As of Dec 2014, this can be done using the unnest function from Hadley Wickham’s tidyr package (see release notes http://blog.rstudio.org/2014/12/08/tidyr-0-2-0/) > library(tidyr) > library(dplyr) > mydf V1 V2 2 1 a,b,c 3 2 a,c 4 3 b,d 5 4 e,f 6 . . > mydf %>% mutate(V2 = strsplit(as.character(V2), “,”)) %>% unnest(V2) V1 V2 … Read more

Transpose / reshape dataframe without “timevar” from long to wide format

With the data.table package, this could easily be solved with the new rowid function: library(data.table) dcast(setDT(d1), Name ~ rowid(Name, prefix = “medication”), value.var = “MedName”) which gives: Name medication1 medication2 medication3 1 Name1 atenolol 25mg aspirin 81mg sildenafil 100mg 2 Name2 atenolol 50mg enalapril 20mg <NA> Another method (commonly used before version 1.9.7): dcast(setDT(d1)[, rn … 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.)

Reshaping data.frame from wide to long format

Three alternative solutions: 1) With data.table: You can use the same melt function as in the reshape2 package (which is an extended & improved implementation). melt from data.table has also more parameters that the melt-function from reshape2. You can for example also specify the name of the variable-column: library(data.table) long <- melt(setDT(wide), id.vars = c(“Code”,”Country”), … Read more