Message-ID: <f8e6ff050904041048h1c584e39x80b49b6e94e9338@mail.gmail.com>
Date: 2009-04-04T17:48:40Z
From: Hadley Wickham
Subject: data.frame, converting row data to columns
In-Reply-To: <644e1f320904041028t3e00ab67wa9e649d452fb0f39@mail.gmail.com>
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
--
http://had.co.nz/