[R] data.frame to "stacked frame"

Christian Schulz chschulz at email.de
Mon Dec 7 14:20:26 CET 2009


Hi,

have anybody a hint how i could avoid the cumbersome way (..especially 
the value part)
change the data representation from  n column data.frame to an stacked 
representation.

many thanks
Christian

#example data
dfw <- as.data.frame(matrix(runif(10*10),ncol=10))
dfw$group <- sample(c("X","Z"),nrow(dfw),replace=T,prob=c(0.75,0.25))

# change
var   <- rep(names(dfw)[-length(dfw)],nrow(dfw))
group <- rep(dfw$group,nrow(dfw))

#cumbersome
value <- vector()
z=1
for(i in 1:(length(dfw)-1)){
for(j in 1:nrow(dfw)){
value[z] <-   dfw[j,i]
z=z+1
 }
}

dfr <- cbind(var,group,value)




More information about the R-help mailing list