[R] question about data manipulation

hadley wickham h.wickham at gmail.com
Sun Dec 10 16:04:25 CET 2006


On 12/10/06, Jenny persson <jenny197806 at yahoo.se> wrote:
>   Dear all,
>
>   I have a dataset
>
>
>   dat<- pep[c(420:423,1258:1261,2096:2099),c(3,4,7,14)]
>
>
>        Slide  Block       Name     pearson_res
>   2102    23     2 CTERQANFLGKIWPS  0.07618407
>   2103    23     2 ATLEEMMTACQGVGG  1.93543619
>   2104    23     2 IPVGEIYKRWIILGL  0.22211959
>   2105    23     2 MFSALSEGATPQDLN -0.08249410
>   3662    24     2 CTERQANFLGKIWPS -0.10250513
>   3663    24     2 ATLEEMMTACQGVGG -0.05479617
>   3664    24     2 IPVGEIYKRWIILGL  0.14669877
>   3665    24     2 MFSALSEGATPQDLN -0.19059432
>   6782    30     2 CTERQANFLGKIWPS -0.01064459
>   6783    30     2 ATLEEMMTACQGVGG -0.03758618
>   6784    30     2 IPVGEIYKRWIILGL  0.20724517
>   6785    30     2 MFSALSEGATPQDLN -0.23034595
>
>
>   where all slides (23,24,30) have the same name. I want to rearrange the dataset so it will have the matrix look
>
>           Name    slide=23 slide=24 slide=30 block
>   CTERQANFLGKIWPS
>   ATLEEMMTACQGVGG
>   IPVGEIYKRWIILGL
>   MFSALSEGATPQDLN
>
>   the pearson_res values will be filled under each slide corresponding to the name-variable.
>
>   I find manipulating data like this is quite tricky. Thanks for your help.

You might want to have a look at the reshape package,
http://had.co.nz/reshape, which aims to make these operations simple.

For your example, something like the following should work:

dm <- melt(dat, id=c("Slide", "Block","Name"))
cast(dm, Name ~ Slide + Block)
cast(dm, Name ~ Block + Slide)

or even
cast(dm, Name ~ Block ~ Slide)
depending on what you want to do with the data next.

Regards,

Hadley




More information about the R-help mailing list