[R] data.frame to "stacked frame"

Ista Zahn istazahn at gmail.com
Tue Dec 8 00:08:44 CET 2009


One option is

library(reshape)
dfr <- melt(dfw, id.vars="group")

-Ista

On Mon, Dec 7, 2009 at 8:20 AM, Christian Schulz <chschulz at email.de> wrote:
> 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)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list