Skip to content
Prev 132357 / 398506 Next

Reshape Dataframe

On Dec 18, 2007 9:07 AM, Bert Jacobs <b.jacobs at pandora.be> wrote:
Try this:

out <- ftable(xtabs(Var4 ~ Var1 + Var2 + Var3, DF))
out[out == 0] <- NA

Omit the last line is 0 fill is what you had wanted.

This will do it except that it will eliminate all rows
without data:

out2 <- reshape(DF, dir = "wide", timevar = "Var3", idvar = c("Var1", "Var2"))
out2[is.na(out2)] <- 0

Omit the last line if NA fill is what you wanted.

The reshape package melt/cast routines (see Hadley's solution in this
thread) can be used
to give a similar result to the reshape command above (i.e. all
missing rows are not
included) except that cast is a bit more flexible since it has a fill= argument.