[R] Transform a dataset from long to wide using reshape2
John Kane
jrkrideau at yahoo.ca
Sun Feb 27 07:21:45 CET 2011
Thanks. I suddenly realised this as I was cycling down to the local grocery store, about 10 minutes after posting the problem. I felt like an idiot. Thanks for confirming my stupidity.
john
--- On Sat, 2/26/11, Phil Spector <spector at stat.berkeley.edu> wrote:
> From: Phil Spector <spector at stat.berkeley.edu>
> Subject: Re: [R] Transform a dataset from long to wide using reshape2
> To: "John Kane" <jrkrideau at yahoo.ca>
> Cc: "R R-help" <r-help at stat.math.ethz.ch>
> Received: Saturday, February 26, 2011, 2:24 PM
> John -
> In the dat data frame, there is exactly one
> observation
> for each Subject/Item combination, so it's clear how to
> rearrange the values of Score. In the mydata data
> frame there
> are multiple values for most combinations of x and id, so
> reshape2 needs to aggregate y before it can rearrange the
> values.
> Hope this helps.
>
> - Phil Spector
>
> Statistical
> Computing Facility
>
> Department
> of Statistics
>
> UC
> Berkeley
>
> spector at stat.berkeley.edu
>
>
>
> On Sat, 26 Feb 2011, John Kane wrote:
>
> > I seem to be running into the same problem reported
> in
> > https://stat.ethz.ch/pipermail/r-help/2010-November/258265.html
> >
> > I cannot seem to transform a dataset from long to wide
> using reshape2.
> > Clearly I am missing something very simple but a look
> at the manual and the reshape paper in JSS does not suggest
> anything.
> >
> > Any advice would be welcome
> > ===========================load
> rehape2====================
> > library(reshape2)
> > ========================Working
> example===========================
> > # Example from above URL
> > dat <-
> read.table(textConnection('Subject Item
> Score
> > "Subject 1" "Item 1" 1
> > "Subject 1" "Item 2" 0
> > "Subject 1" "Item 3" 1
> > "Subject 2" "Item 1" 1
> > "Subject 2" "Item 2" 1
> > "Subject 2" "Item 3" 0'),
> header=TRUE)
> > closeAllConnections()
> >
> > acast(dat, Subject~Item)
> >
> > # Seems fine
> > =====================My
> attemp===========================
> > #
> > set.seed(1)
> > mydata <- data.frame( x=sample(LETTERS[23:26],100,
> replace=TRUE),
> > y=rnorm(100, mean=2),
> id=sample(letters[1:4], 100, replace=TRUE))
> >
> > acast(mydata , id ~ x)
> > # not so fine
> >
> =================================================================
> > produces this :
> > Using id as value column: use value_var to override.
> > Aggregation function missing: defaulting to length
> > W X Y Z
> > a 7 5 5 7
> > b 6 9 6 12
> > c 1 4 5 6
> > d 6 14 5 2
> >
> > Why does reshape2 think it needs an aggregation
> function here>
> >
> > Thanks
> >
> > ______________________________________________
> > 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