[R] Reorganize data fram

Dennis Murphy djmuser at gmail.com
Fri Jul 15 18:03:02 CEST 2011


Hi:

On Fri, Jul 15, 2011 at 6:04 AM, anglor <angelica.ekenstam at dpes.gu.se> wrote:
> Hi, thanks for your reply.
>
> I didn't get cast() to work and didn't know how to find information about it
> either.

 Hadley Wickham's home page http://had.co.nz/ has a link (last one
under the heading 'R Packages') to the reshape package page, where
some documentation resides.

Could you post a representative sample of your input data frame using
dput()? Here's an example:
df <- data.frame(x = 1:4, y = rnorm(4), z = rpois(4, 3))
> dput(df)
structure(list(x = 1:4, y = c(-0.49491054748322, -1.53013240418216,
0.0189088048735591, -0.0766510981813545), z = c(2, 2, 3, 4)), .Names = c("x",
"y", "z"), row.names = c(NA, -4L), class = "data.frame")

Copy and paste the result of dput() into your e-mail. This is the
preferred way to transport data that is readable on all platforms
while guaranteeing that a potential R-helper sees the same data
structure you do.

Clearly, you don't want to send 700,000 observations with dput(), but
a small sample that is sufficient to illustrate the problem is
desirable. If possible, also send the code that you tried and the
expected result, as you did in your initial post.

 I used reshape but then I had to subset only those columns (actually
> I have 28 columns of other data) Could cast or reshape work also with more
> columns?

Are these columns 'constant' apart from Temperature? If so, then the
following should work, but this needs the 'new and improved' reshape2
package instead. I'm using the same data frame d as before with a
couple added 'constant' variables:

d$age <- 12
d$region <- 'NW'
d$zone <- 'CET'
d
     Date Temperature Category age region zone
1 2007102          16        A  12     NW  CET
2 2007102          17        B  12     NW  CET
3 2007102          18        C  12     NW  CET

library(reshape2)
dcast(d, ... ~ Category, value_var = 'Temperature')
     Date age region zone  A  B  C
1 2007102  12     NW  CET 16 17 18

If they're not (all) constant, then you need to post some data per
above and describe your desired outcome.

HTH,
Dennis

>
> Angelica
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3669899.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.
>



More information about the R-help mailing list