[R] Help with Reshaping from Wide to Long

Phil Spector spector at stat.berkeley.edu
Sat Jul 17 23:08:24 CEST 2010


An alternative using the base reshape function:

one = reshape(accuracy,idvar='Subject',varying=list(c(2,3,4),c(5,6,7),c(8,9,10)),
               direction='long',timevar='shape')
two = reshape(one,idvar=c('Subject','shape'),varying=list(3:5),
               direction='long',timevar='color')
two$shape=factor(two$shape,labels=c('Circle','Square','Triangle'))
two$color=factor(two$color,labels=c('Blue','Red','Green'))
names(two)[4] = 'value'

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Sat, 17 Jul 2010, Jeff Newmiller wrote:

> Try this:
>
> library(reshape)
>
> accuracy <- structure(list(Subject = c(101L, 102L, 103L, 104L, 105L, 106L
> ), CircleBlue = c(95L, 80L, 80L, 85L, 70L, 70L), CircleRed = c(100L,
> 90L, 70L, 80L, 75L, 75L), CircleGreen = c(100L, 100L, 95L, 100L,
> 95L, 75L), SquareBlue = c(95L, 85L, 90L, 90L, 70L, 40L), SquareRed =
> c(100L,
> 90L, 100L, 90L, 75L, 60L), SquareGreen = c(100L, 100L, 100L, 90L,
> 85L, 85L), TriangleBlue = c(60L, 55L, 65L, 65L, 60L, 40L), TriangleRed =
> c(80L,
> 45L, 60L, 50L, 40L, 35L), TriangleGreen = c(75L, 45L, 55L, 50L, 45L,
> 50L)), .Names = c("Subject", "CircleBlue", "CircleRed", "CircleGreen",
> "SquareBlue", "SquareRed", "SquareGreen", "TriangleBlue", "TriangleRed",
> "TriangleGreen"), row.names = c(NA, 6L), class = "data.frame")
>
> tmp1 <- melt( accuracy, id="Subject" )
> colnames( tmp1 ) [ which( colnames( tmp1 ) == "value" ) ] <- "Accuracy"
>
> keys <- data.frame( variable = levels( tmp1$variable )
>                  , Shape=rep( c( "Circle", "Square", "Triangle" ), each=3 )
>                  , Color=rep( c( "Blue", "Red", "Green" ), times=3 )
>                  )
> tmp2 <- merge( tmp1, keys )
>
> accuracym <- tmp2[ , c("Accuracy", "Subject", "Shape", "Color") ]
>
> On Sat, 17 Jul 2010, John L. Woodard wrote:
>
>> Hi Tal,
>> 
>> Here is the output as you requested:
>> 
>> structure(list(Subject = c(101L, 102L, 103L, 104L, 105L, 106L
>> ), CircleBlue = c(95L, 80L, 80L, 85L, 70L, 70L), CircleRed = c(100L,
>> 90L, 70L, 80L, 75L, 75L), CircleGreen = c(100L, 100L, 95L, 100L,
>> 95L, 75L), SquareBlue = c(95L, 85L, 90L, 90L, 70L, 40L), SquareRed =
>> c(100L,
>> 90L, 100L, 90L, 75L, 60L), SquareGreen = c(100L, 100L, 100L, 90L,
>> 85L, 85L), TriangleBlue = c(60L, 55L, 65L, 65L, 60L, 40L), TriangleRed =
>> c(80L,
>> 45L, 60L, 50L, 40L, 35L), TriangleGreen = c(75L, 45L, 55L, 50L, 45L,
>> 50L)), .Names = c("Subject", "CircleBlue", "CircleRed", "CircleGreen",
>> "SquareBlue", "SquareRed", "SquareGreen", "TriangleBlue", "TriangleRed",
>> "TriangleGreen"), row.names = c(NA, 6L), class = "data.frame")
>> 
>> Regarding the ANOVA, OK--I see it now (I was looking for the words
>> "Repeated Measures", which weren't there...).  I didn't notice the
>> example till I ran each one.  It looks like a great program, though I'll
>> have to figure out the reshape issue before I can use ezANOVA.  Many
>> thanks for your help!
>> 
>> John
>> 
>> John L. Woodard, Ph.D.
>> 
>> Associate Professor of Psychology
>> 
>> Wayne State University
>> 
>> 5057 Woodward Ave., 7th Floor
>> 
>> Detroit, MI 48202
>> 
>> 
>> Voice: 313-577-5838
>> 
>> Fax: 313-577-7636
>> 
>> e-mail: john.woodard at wayne.edu
>> 
>> 
>> On 7/17/10 2:40 PM, Tal Galili wrote:
>>> Hi John,
>>> 
>>> Try posting a sample of your data in reply to this e-mail by using:
>>> 
>>> dput(head(accuracy))
>>> 
>>> And me (or someone else) will be sure to fix your command.
>>> 
>>> Regarding the ANOVA, read more :)
>>> 
>>> Tal
>>> 
>>> 
>>> 
>>> ----------------Contact
>>> Details:-------------------------------------------------------
>>> Contact me: Tal.Galili at gmail.com <mailto:Tal.Galili at gmail.com> |
>>> 972-52-7275845
>>> Read me: www.talgalili.com <http://www.talgalili.com> (Hebrew) |
>>> www.biostatistics.co.il <http://www.biostatistics.co.il> (Hebrew) |
>>> www.r-statistics.com <http://www.r-statistics.com> (English)
>>> 
>>> ----------------------------------------------------------------------------------------------
>>> 
>>> 
>>> 
>>> 
>>> On Sat, Jul 17, 2010 at 8:03 PM, jlwoodard <john.woodard at wayne.edu
>>> <mailto:john.woodard at wayne.edu>> wrote:
>>> 
>>>
>>>     Tal,
>>>      Thanks for the information.
>>>
>>>      I actually did read through the help for the reshape package,
>>>     though being
>>>     relatively new to R, I don't quite understand the ins and outs of the
>>>     command.
>>>
>>>     I tried using the melt command:
>>>     x<-melt(accuracy,id='Subject')
>>>
>>>     but, it didn't give me anything different than the stacked
>>>     command.  I'm
>>>     trying to get two columns to indicate the Shape and Color.
>>>
>>>     Thanks also for this information on ezANOVA.  It looks very promising,
>>>     though I didn't see an example of repeated measures ANOVA in the
>>>     help file.
>>>
>>>     Best regards,
>>>
>>>     John
>>>     --
>>>     View this message in context:
>>>     http://r.789695.n4.nabble.com/Help-with-Reshaping-from-Wide-to-Long-tp2292462p2292524.html
>>>     Sent from the R help mailing list archive at Nabble.com.
>>>
>>>     ______________________________________________
>>>     R-help at r-project.org <mailto: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.
>>> 
>>> 
>>
>> 	[[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.
>> 
>
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> 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