Hi - I'm up against a complicated reshape problem. I have data of the form X1,Y1,hr1,hr2,hr3 X1,Y2,hr1,hr2,hr3 X1,Y3,hr1,hr2,hr3 X2,Y1,hr1,hr2,hr3 X2,Y2,hr1,hr2,hr3 X2,Y3,hr1,hr2,hr3 where X and Y are factors and the hr(1,2,3) are values. I need it as ,X1, X2 Y1,hr1,hr1 Y1,hr2,hr2 Y1,hr3,hr3 Y2,hr1,hr1 Y2,hr2,hr2 Y2,hr3,hr3 .., Any hints? I've been at it for hours. p -- View this message in context: http://r.789695.n4.nabble.com/Reshape-tp3224455p3224455.html Sent from the R help mailing list archive at Nabble.com.
This gets you close; just edit the row names:> x <- read.table(textConnection("X1,Y1,hr1,hr2,hr3+ X1,Y2,hr1,hr2,hr3 + X1,Y3,hr1,hr2,hr3 + X2,Y1,hr1,hr2,hr3 + X2,Y2,hr1,hr2,hr3 + X2,Y3,hr1,hr2,hr3"), sep = ',', as.is = TRUE)> closeAllConnections() > require(reshape2) > x.m <- melt(x, id = c('V1', 'V2'), stringsAsFactors = FALSE) > t(cast(x.m, V1 ~ V2, fun = as.character))X1 X2 Y1_X1 "hr1" "hr1" Y1_X2 "hr2" "hr2" Y1_X3 "hr3" "hr3" Y2_X1 "hr1" "hr1" Y2_X2 "hr2" "hr2" Y2_X3 "hr3" "hr3" Y3_X1 "hr1" "hr1" Y3_X2 "hr2" "hr2" Y3_X3 "hr3" "hr3">On Tue, Jan 18, 2011 at 9:18 PM, pwilliam <pwilliam at uoregon.edu> wrote:> > Hi - I'm up against a complicated reshape problem. ?I have data of the form > > X1,Y1,hr1,hr2,hr3 > X1,Y2,hr1,hr2,hr3 > X1,Y3,hr1,hr2,hr3 > X2,Y1,hr1,hr2,hr3 > X2,Y2,hr1,hr2,hr3 > X2,Y3,hr1,hr2,hr3 > > where X and Y are factors and the hr(1,2,3) are values. ?I need it as > ? ?,X1, X2 > Y1,hr1,hr1 > Y1,hr2,hr2 > Y1,hr3,hr3 > Y2,hr1,hr1 > Y2,hr2,hr2 > Y2,hr3,hr3 > .., > > Any hints? I've been at it for hours. > > p > > -- > View this message in context: http://r.789695.n4.nabble.com/Reshape-tp3224455p3224455.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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: Here's a variation on Jim Holtman's solution - same packages. df <- data.frame(V1 = rep(c('X1', 'X2'), each = 3), V2 = rep(c('Y1', 'Y2', 'Y3'), 2), Y1 = rep(1, 6), Y2 = rep(2, 6), Y3 = rep(3, 6)) dd <- melt(df, id = c('V1', 'V2')) dcast(dd, V2 + variable ~ V1) # A little better... dcast(dd, V2 + variable ~ V1)[, -2] V2 X1 X2 1 Y1 1 1 2 Y1 2 2 3 Y1 3 3 4 Y2 1 1 5 Y2 2 2 6 Y2 3 3 7 Y3 1 1 8 Y3 2 2 9 Y3 3 3 HTH, Dennis On Tue, Jan 18, 2011 at 6:18 PM, pwilliam <pwilliam@uoregon.edu> wrote:> > Hi - I'm up against a complicated reshape problem. I have data of the form > > X1,Y1,hr1,hr2,hr3 > X1,Y2,hr1,hr2,hr3 > X1,Y3,hr1,hr2,hr3 > X2,Y1,hr1,hr2,hr3 > X2,Y2,hr1,hr2,hr3 > X2,Y3,hr1,hr2,hr3 > > where X and Y are factors and the hr(1,2,3) are values. I need it as > ,X1, X2 > Y1,hr1,hr1 > Y1,hr2,hr2 > Y1,hr3,hr3 > Y2,hr1,hr1 > Y2,hr2,hr2 > Y2,hr3,hr3 > .., > > Any hints? I've been at it for hours. > > p > > -- > View this message in context: > http://r.789695.n4.nabble.com/Reshape-tp3224455p3224455.html > Sent from the R help mailing list archive at Nabble.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. >[[alternative HTML version deleted]]
Seemingly Similar Threads
- lm coefficients output confusing
- broken mailing-list -> Re: Accentuated characters issue when receiving attributes from "samba user syncpasswords"
- lists.samba.org's Mail Servers Are A Bit Wonky?
- Updated llc does not compile my .ll files any more [addrspace on AVR problem?]
- Updated llc does not compile my .ll files any more [addrspace on AVR problem?]