[R] RE SHAPE package question.

hadley wickham h.wickham at gmail.com
Sat Aug 2 23:30:27 CEST 2008


On Sat, Aug 2, 2008 at 4:00 PM, hpdutra <hpdutra at yahoo.com> wrote:
>
> I am sorry but here is an example
>> test
>  t1 t2 t3 t4 M F id
> 1  1  0  0  0 1 0  1
> 2  1  0  0  0 1 0  1
> 3  1  0  0  0 0 1  1
> 4  1  0  1  1 1 0  1
> 5  1  0  1  1 1 0  1
> 6  1  0  1  1 1 0  1
> 7  1  0  1  1 0 1  1
>
>> test<-read.table(h=T, file.choose())
>> dfm <- melt(test, m = "id")
>> summarised <- cast(dfm, t1+ t2 + t3 + t4 + M + F ~ variable, sum)
>> summarised
>  t1 t2 t3 t4 M F id
> 1  1  0  0  0 0 1  1
> 2  1  0  0  0 1 0  2
> 3  1  0  1  1 0 1  1
> 4  1  0  1  1 1 0  3
>
>
>
> So it works partially because it does not add the male and females columns.
> I am sure it is simple but I just don't know how to do it. I was hoping that
> it would give me the following data

I think that you need to treat M and F as measured variables as well:

dfm <- melt(test, m = c("id", "M", "F"))

Then the same cast command will get you the format that you want.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list