data.frame, converting row data to columns
On Sat, Apr 4, 2009 at 12:28 PM, jim holtman <jholtman at gmail.com> wrote:
Does this do what you want:
x <- read.table(textConnection("name ? ? ? ? wrist nLevel ? ? ? ? ? ?emot
+ 1 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ?3.34 ? ? ? ? ? ? ? ? ? ?1 ? frustrated + 2 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ?3.94 ? ? ? ? ? ? ? ? ? ?1 ?frustrated + 3 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ? ?NA ? ? ? ? ? ? ? ? ? ?1 ? frustrated + 4 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ?3.51 ? ? ? ? ? ? ? ? ? ?1 ? frustrated + 5 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ?3.81 ? ? ? ? ? ? ? ? ? ?1 ? frustrated + 6 ? ? ? ? ? ? ? ? ? ?4101 ? ? ? ? ?2.62 ? ? ? ? ? ? ? ? ? ?4 ? excited + 7 ? ? ? ? ? ? ? ? ? ?4094 ? ? ? ? ?2.65 ? ? ? ? ? ? ? ? ? ?1 ? frustrated + 8 ? ? ? ? ? ? ? ? ? ?4101 ? ? ? ? ? ?NA ? ? ? ? ? ? ? ? ? ?4 ? excited + 9 ? ? ? ? ? ? ? ? ? ?4101 ? ? ? ? ?0.24 ? ? ? ? ? ? ? ? ? ?4 ? excited + 10 ? ? ? ? ? ? ? ? ? 4101 ? ? ? ? ?0.23 ? ? ? ? ? ? ? ? ? ?4 excited"), header=TRUE)
# add index x$indx <- ave(seq_along(x$emot), x$emot, FUN=function(z) seq(length(z))) require(reshape) y <- melt(x, measure='wrist') cast(y, name+nLevel+emot~indx)
which you can abbreviate to : cast(y, ... ~ indx) ... means all the other variables not explicitly mentioned. Hadley