Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reshaping data (a faster way)

Tags:

r

data.table

plyr

I came across a table of freq. counts today I had to expand into a data frame of raw values. I was able to do it but was wondering if there's a faster way using the reshape package or data.table?

The original table looked like this:

   i1 i2 i3 i4  m  f
1   0  0  0  0 22 29
2   1  0  0  0 30 50
3   0  1  0  0 13 15
4   0  0  1  0  1  6
5   1  1  0  0 24 67
6   1  0  1  0  5 12
7   0  1  1  0  1  2
8   1  1  1  0 10 22
9   0  0  0  1 10  7
10  1  0  0  1 27 30
11  0  1  0  1 14  4
12  0  0  1  1  1  0
13  1  1  0  1 54 63
14  1  0  1  1  8 10
15  0  1  1  1  8  6
16  1  1  1  1 57 51

Here's an easy grab of the data using dput:

dat <- structure(list(i1 = c(0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 
0L, 0L, 1L, 1L, 0L, 1L), i2 = c(0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 
0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L), i3 = c(0L, 0L, 0L, 1L, 0L, 1L, 
1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L), i4 = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), m = c(22L, 30L, 
13L, 1L, 24L, 5L, 1L, 10L, 10L, 27L, 14L, 1L, 54L, 8L, 8L, 57L
), f = c(29L, 50L, 15L, 6L, 67L, 12L, 2L, 22L, 7L, 30L, 4L, 0L, 
63L, 10L, 6L, 51L)), .Names = c("i1", "i2", "i3", "i4", "m", 
"f"), class = "data.frame", row.names = c(NA, -16L))

My approach(s) to reshape the data (is there a faster way?):

#step 1: method 1 (in this case binding and stacking uses less code than reshape)
dat2 <- data.frame(rbind(dat[,1:4], dat[, 1:4]), 
    sex = rep(c('m', 'f'), each=16),
    n = c(dat$m, dat$f))
dat2

#step 1: method 2    
dat3 <- reshape(dat, direction = "long", idvar = 1:4,
    varying = list(c("m", "f")),
    v.names = c("n"),
    timevar = "sex",
    times = c("m", "f"))
    rownames(dat3) <- 1:nrow(dat3)
    dat3 <- data.frame(dat3)
    dat3$sex <- as.factor(dat3$sex)

all.equal(dat3, dat2) #just to show both method 1 and 2 give the same data frame

#step 2
dat4 <- dat2[rep(seq_len(nrow(dat2)), dat2$n), 1:5]
rownames(dat4) <- 1:nrow(dat4)
dat4

I assume this is a common problem as when you want to take a table from an article and reproduce it, it requires some unpacking. I am finding myself doing this more and more and want to make sure I'm being efficient.

like image 840
Tyler Rinker Avatar asked Mar 30 '12 01:03

Tyler Rinker


3 Answers

Here is a one-liner.

dat2 <- ddply(dat, 1:4, summarize, sex = c(rep('m', m), rep('f', f)))
like image 104
Ramnath Avatar answered Oct 13 '22 09:10

Ramnath


And here's a base R one-liner.

dat2 <- cbind(dat[c(rep(1:nrow(dat), dat$m), rep(1:nrow(dat), dat$f)),1:4],
              sex=c(rep("m",sum(dat$m)), rep("f", sum(dat$f))))

Or, a little more generally:

d1 <- dat[,1:4]
d2 <- as.matrix(dat[,5:6])
dat2 <- cbind(d1[rep(rep(1:nrow(dat), ncol(d2)), d2),], 
              sex=rep(colnames(d2), colSums(d2)))
like image 28
Aaron left Stack Overflow Avatar answered Oct 13 '22 09:10

Aaron left Stack Overflow


Given that nobody has posted a data.table solution (as suggested in the original question)

library(data.table)
DT <- as.data.table(dat)   
DT[,list(sex = rep(c('m','f'),c(m,f))), by=  list(i1,i2,i3,i4)]

Or, even more succinctly

DT[,list(sex = rep(c('m','f'),c(m,f))), by=  'i1,i2,i3,i4']
like image 3
mnel Avatar answered Oct 13 '22 08:10

mnel