[R] reshape data from long to wide format

Dennis Murphy djmuser at gmail.com
Fri Sep 9 19:29:45 CEST 2011


Hi:

There are two ways to go about this, depending on what you want.

# 1: Jean's output format:

# I'm using the reshape package here with cast(),
# but it should work similarly with dcast() in reshape2:
> cast(example, DATE ~ SENSOR, value = 'VALUE')
           DATE   A     B  C  D     E   F
1  01/01/2010 1 270    NA NA NA    NA  NA
2  01/01/2010 2  NA 292.5 NA NA    NA  NA
3  01/01/2010 3  NA    NA  0 NA    NA  NA
4  01/01/2010 4  NA    NA NA 45    NA  NA
5  01/01/2010 5  NA    NA NA NA 247.5  NA
6  01/01/2010 6  NA    NA NA NA    NA 315

# Method 2: One line per day - requires redefinition of date
example$date <- as.Date('01/01/2010', format = '%m/%d/%Y')
cast(example, date ~ SENSOR, value = 'VALUE')
        date   A     B C  D     E   F
1 2010-01-01 270 292.5 0 45 247.5 315

In both cases, note the use of value = 'VALUE' in the cast() call. It
needs value_var = 'VALUE' instead of value = 'VALUE' if you're using
dcast() from reshape2 instead:

library('reshape2')
dcast(example, DATE ~ SENSOR, value_var = 'VALUE')
dcast(example, date ~ SENSOR, value_var = 'VALUE')

HTH,
Dennis

On Fri, Sep 9, 2011 at 4:28 AM, maxbre <mbressan at arpa.veneto.it> wrote:
> This is my reproducible example:
>
> example<-structure(list(SENSOR = structure(1:6, .Label = c("A", "B", "C",
> "D", "E", "F"), class = "factor"), VALUE = c(270, 292.5, 0, 45,
> 247.5, 315), DATE = structure(1:6, .Label = c(" 01/01/2010 1",
> " 01/01/2010 2", " 01/01/2010 3", " 01/01/2010 4", " 01/01/2010 5",
> " 01/01/2010 6"), class = "factor")), .Names = c("SENSOR", "VALUE",
> "DATE"), class = "data.frame", row.names = c("1", "2", "3", "4",
> "5", "6"))
>
> I need to resahpe "example"  in a wide format so that “SENSOR” appear as
> columns and “DATE” as rows with corresponding “VALUE” as value;
>
> I thought it was very simple so that I’ve been trying this:
>
> dcast(example,DATE~SENSOR)
>
> But I've got this message:
>
> Using DATE as value column.  Use the value argument to cast to override this
> choice
> Errore in `[.data.frame`(data, , variables, drop = FALSE) :
>  undefined columns selected
>
> and even if by using the value argument sorted out any good result…
>
> sorry for the very trivial question, I've been looking at documentation and
> forums anywhere but I was not successful at all and now I’m somehow in the
> right middle of nowhere…
>
> any help or hint for this?
>
> thank you
>
> max
>
> --
> View this message in context: http://r.789695.n4.nabble.com/reshape-data-from-long-to-wide-format-tp3801381p3801381.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