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:> datx1 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> durd1 d2 d3 1 a d g 2 b e h 3 c f i [[alternative HTML version deleted]]
Here is one way of doing it:> xx1 x2 row 1 1 a 1 2 1 b 2 3 1 c 3 4 2 d 1 5 2 e 2 6 2 f 3 7 3 g 1 8 3 h 2 9 3 i 3> # create indices for new table > x$row <- ave(x$x1, x$x1, FUN=seq_along) > # create output matrix > result <- matrix('', max(x$row), max(x$x1)) > result[cbind(x$row, x$x1)] <- x$x2 > result[,1] [,2] [,3] [1,] "a" "d" "g" [2,] "b" "e" "h" [3,] "c" "f" "i" On Sun, Feb 6, 2011 at 6:13 AM, Andr? de Boer <rnieuws at gmail.com> wrote:> 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
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.
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.