[R] manipulate dataframe

Victor F Seabra vseabra at uol.com.br
Sun Feb 6 16:27:33 CET 2011


   another option is to use reshape()
   x<-data.frame(x1=rep(1:3,each=3),x2=letters[1:9])
   x$id<-rep(1:3,3)
   dur<-reshape(x,timevar="x1",idvar="id",direction="wide")
   dur<-dur[,-1]
   colnames(dur) <- paste("d", unique(x$x1), sep="")
   dur
   cheers, Victor
     _________________________________________________________________

   Em 06/02/2011 12:50, Patrick Hausmann < patrick.hausmann at uni-bremen.de >
   escreveu:
   Hi André,
   try this:
   df1 <- data.frame(x1 = rep(1:3, each=3), x2=letters[1:9])
   dfs <- split(df1, df1$x1)
   df2 <- data.frame(sapply(dfs, FUN="[[", "x2"))
   colnames(df2) <- paste("d", unique(df1$x1), sep="")
   df2
   HTH
   Patrick
   Am 06.02.2011 12:13, schrieb André de Boer:
   > Hello,
   >
   > Can someone give me hint to change a data.frame.
   > I want to split a column in more columns depending on the value of a other
   > column.
   > Thanks for the reaction,
   > Andre
   >
   > Example:
   >> dat
   > x1 x2
   > 1 1 a
   > 2 1 b
   > 3 1 c
   > 4 2 d
   > 5 2 e
   > 6 2 f
   > 7 3 g
   > 8 3 h
   > 9 3 i
   >
   > in
   >
   >> dur
   > d1 d2 d3
   > 1 a d g
   > 2 b e h
   > 3 c f i
   >
   > [[alternative HTML version deleted]]
   >
   > ______________________________________________
   > 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.
   ______________________________________________
   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.


More information about the R-help mailing list