[R] data.frame to "stacked frame"

Henrique Dallazuanna wwwhsd at gmail.com
Mon Dec 7 14:30:58 CET 2009


Try this:

cbind(stack(dfw, select = -group), dfw$group)

On Mon, Dec 7, 2009 at 11: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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list