[R] Reshape

jim holtman jholtman at gmail.com
Wed Jan 19 05:20:05 CET 2011


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?



More information about the R-help mailing list