[R] Transformation of a data frame

hadley wickham h.wickham at gmail.com
Wed Sep 13 13:57:27 CEST 2006


> 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



More information about the R-help mailing list