Re: [R] Transformation of a data frame

From: hadley wickham <h.wickham_at_gmail.com>
Date: Wed 13 Sep 2006 - 11:57:27 GMT

> one approach is to use reshape(), e.g.,
>
> # suppose that 'dat' is your data.frame, then
> res <- reshape(dat, direction = "wide", idvar = "Q", timevar = "S")
> res[is.na(res)] <- 0
> res

You can also use the reshape package:

library(reshape)
datm <- melt(dat, id=1:2)
cast(datm, Q ~ S)

See the introduction to reshape
(http://www.had.co.nz/reshape/introduction.pdf) for more details and examples.

Hadley



R-help@stat.math.ethz.ch 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. Received on Wed Sep 13 22:00:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 13 Sep 2006 - 12:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.