Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape R: split a column

Tags:

r

reshape

A really simple question but I could'nt find a solution: I have a 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
A  D  10
A  D  11
B  E  12
B  E  13
C  F  14
C  F  15

And I would like

V1 V2 V3.1 V3.2
A  D   10   11
B  E   12   13
C  F   14   15

I try reshape{stats} and reshape2

like image 537
nebi Avatar asked Mar 22 '23 12:03

nebi


2 Answers

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

Mark Miller shows the base R approach, and creates the time variable manually.

Here's a way to automatically create the time variable, and the equivalent command for dcast from the "reshape2" packge:

## 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  A  D 10 11
# 2  B  E 12 13
# 3  C  F 14 15

Self-promotion alert

Since this type of question has cropped up several times, and since a lot of datasets don't always have a unique ID, I have implemented a variant of the above as a function called getanID in my "splitstackshape" package. In its present version, it hard-codes the name of the "time" variable as ".id". If you were using that, the steps would be:

library(splitstackshape)
library(reshape2)
df <- getanID(df, id.vars=c("V1", "V2"))
dcast(df, V1 + V2 ~ .id, value.var = "V3")
like image 181
A5C1D2H2I1M1N2O1R2T1 Avatar answered Apr 02 '23 02:04

A5C1D2H2I1M1N2O1R2T1


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  A  D   10   11
3  B  E   12   13
5  C  F   14   15
like image 31
Mark Miller Avatar answered Apr 02 '23 02:04

Mark Miller