Re: [R] Change dataframe-structure
There is probably a more ellegant way, but: > df <- data.frame(p1=c(1,2,1),p2=c(3,3,2),p3=c(2,1,3),p4=c(5,6,4),p5=c(4,4,6),p6=c(6,5,5)) > as.data.frame(t(apply(df,1,function(x) names(x)[match(1:6,x)]))) V1 V2 V3 V4 V5 V6 1 p1 p3 p2 p5 p4 p6 2 p3 p1 p2 p5 p6 p4 3 p1 p2 p3 p4 p6 p5 > On Mon, Feb 13, 2012 at 2:07 PM, David Studer wrote: > Hello everybody, > > I have the following problem and have no idea how to solve it: > > In my dataframe I have six columns representing six societal problems (p1, > p2, ..., p6). > The values are ranks between 1 (worst problem) and 6 (best problem) > > > p1 p2 p3 p4 p5 p6 > 1 3 2 5 4 6 > 2 3 1 6 4 5 > 1 2 3 4 6 5 > > but I'd like the dataframe the other way round: > 123456 > p1 p3 p2 p4 p4 p6 > p3 p1 p2 p5 p6 p4 > p1 p2 p3 p4 p6 p5 > > Can anyone help? > > Thanks! > >[[alternative HTML version deleted]] > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Change dataframe-structure
P <- paste("P",1:6,sep="") t(apply(yourdataframe,1,function(x)P[order(x)])) ## result is a mtrix, though. -- Bert On Mon, Feb 13, 2012 at 2:07 PM, David Studer wrote: > Hello everybody, > > I have the following problem and have no idea how to solve it: > > In my dataframe I have six columns representing six societal problems (p1, > p2, ..., p6). > The values are ranks between 1 (worst problem) and 6 (best problem) > > > p1 p2 p3 p4 p5 p6 > 1 3 2 5 4 6 > 2 3 1 6 4 5 > 1 2 3 4 6 5 > > but I'd like the dataframe the other way round: > 1 2 3 4 5 6 > p1 p3 p2 p4 p4 p6 > p3 p1 p2 p5 p6 p4 > p1 p2 p3 p4 p6 p5 > > Can anyone help? > > Thanks! > > [[alternative HTML version deleted]] > > __ > R-help@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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ R-help@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.
Re: [R] Change dataframe-structure
On Mon, Feb 13, 2012 at 5:07 PM, David Studer wrote: > Hello everybody, > > I have the following problem and have no idea how to solve it: > > In my dataframe I have six columns representing six societal problems (p1, > p2, ..., p6). > The values are ranks between 1 (worst problem) and 6 (best problem) > > > p1 p2 p3 p4 p5 p6 > 1 3 2 5 4 6 > 2 3 1 6 4 5 > 1 2 3 4 6 5 > > but I'd like the dataframe the other way round: > 1 2 3 4 5 6 > p1 p3 p2 p4 p4 p6 > p3 p1 p2 p5 p6 p4 > p1 p2 p3 p4 p6 p5 > First we read the data and then rearrange it into long form (DF) and turn that into a 2d matrix (tapply): Lines <- "p1 p2 p3 p4 p5 p6 1 3 2 5 4 6 2 3 1 6 4 5 1 2 3 4 6 5" DF0 <- read.table(text = Lines, header = TRUE) DF <- as.data.frame.table(as.matrix(DF0), stringsAsFactors = FALSE, responseName = "Ranks") tapply(DF[["Var2"]], DF[-2], c) The result of the last statement is: Ranks Var1 123456 A "p1" "p3" "p2" "p5" "p4" "p6" B "p3" "p1" "p2" "p5" "p6" "p4" C "p1" "p2" "p3" "p4" "p6" "p5" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@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] Change dataframe-structure
Hello everybody, I have the following problem and have no idea how to solve it: In my dataframe I have six columns representing six societal problems (p1, p2, ..., p6). The values are ranks between 1 (worst problem) and 6 (best problem) p1 p2 p3 p4 p5 p6 1 3 2 5 4 6 2 3 1 6 4 5 1 2 3 4 6 5 but I'd like the dataframe the other way round: 123456 p1 p3 p2 p4 p4 p6 p3 p1 p2 p5 p6 p4 p1 p2 p3 p4 p6 p5 Can anyone help? Thanks! [[alternative HTML version deleted]] __ R-help@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.