Reshape R: split column

A very simple question, but I could not find a solution: I have data.frame, like

V1 <- c("A","A","B","B","C","C") V2 <- c("D","D","E","E","F","F") V3 <- c(10:15) df <- data.frame(cbind(V1,V2,V3)) 

i.e.

 V1 V2 V3 AD 10 AD 11 BE 12 BE 13 CF 14 CF 15 

And I would like

 V1 V2 V3.1 V3.2 AD 10 11 BE 12 13 CF 14 15 

I am trying to change form {stats} and reshape2

+4
source share
2 answers

As I mentioned, all you need is a time variable, and you should be fine.

Mark Miller shows the basic approach of R and creates a time variable manually.

Here you can automatically create a time variable and an equivalent command for dcast from the "reshape2" package:

 ## Creating the "time" variable. This does not depend ## on the rows being in a particular order before ## assigning the variables df <- within(df, { A <- do.call(paste, df[1:2]) time <- ave(A, A, FUN = seq_along) rm(A) }) ## This is the "reshaping" step library(reshape2) dcast(df, V1 + V2 ~ time, value.var = "V3") # V1 V2 1 2 # 1 AD 10 11 # 2 BE 12 13 # 3 CF 14 15 

Self-Ad Warning

Since this type of question arises several times, and since many datasets do not always have a unique identifier, I implemented the above option as a function named getanID in my package β€œsplitstackshape”. In its current version, it hardcodes the variable name β€œtime” as β€œ.” id ". If you use this, follow these steps:

 library(splitstackshape) library(reshape2) df <- getanID(df, id.vars=c("V1", "V2")) dcast(df, V1 + V2 ~ .id, value.var = "V3") 
+4
source
 V1 <- c("A","A","B","B","C","C") V2 <- c("D","D","E","E","F","F") V3 <- c(10:15) time <- rep(c(1,2), 3) df <- data.frame(V1,V2,V3,time) df reshape(df, idvar = c('V1','V2'), timevar='time', direction = 'wide') V1 V2 V3.1 V3.2 1 AD 10 11 3 BE 12 13 5 CF 14 15 
+3
source

Source: https://habr.com/ru/post/1501193/


All Articles