[R] cast in reshape and reshape2

David Winsemius dwinsemius at comcast.net
Sat Dec 24 00:06:18 CET 2011


On Dec 23, 2011, at 5:58 PM, David Winsemius wrote:

>
> On Dec 23, 2011, at 2:58 PM, Kaiyin Zhong wrote:
>
>>> library(reshape2)
>>> x = melt(airquality, id=c('month', 'day'))
>>
>> With reshape I can cast with multiple functions:
>>
>>> library(reshape)
>>> cast(x, month+variable~., c(mean,sd))
>>  month variable       mean         sd
>> 1      5    ozone  23.615385  22.224449
>> 2      5  solar.r 181.296296 115.075499
>> 3      5     wind  11.622581   3.531450
>> 4      5     temp  65.548387   6.854870
>> 5      6    ozone  29.444444  18.207904
>> 6      6  solar.r 190.166667  92.882975
>> 7      6     wind  10.266667   3.769234
>> 8      6     temp  79.100000   6.598589
>> 9      7    ozone  59.115385  31.635837
>> 10     7  solar.r 216.483871  80.568344
>> 11     7     wind   8.941935   3.035981
>> 12     7     temp  83.903226   4.315513
>> 13     8    ozone  59.961538  39.681210
>> 14     8  solar.r 171.857143  76.834943
>> 15     8     wind   8.793548   3.225930
>> 16     8     temp  83.967742   6.585256
>> 17     9    ozone  31.448276  24.141822
>> 18     9  solar.r 167.433333  79.118280
>> 19     9     wind  10.180000   3.461254
>> 20     9     temp  76.900000   8.355671
>>
>> Is there a way to do the same job with reshape2?
>
> Have you looked at the .summarise argument to dcast? That seems to  
> deliver the same sort of results one gets with base::aggregate.

Actually I see after looking at examples on the plyr-reshape- 
googlegroups group that it is not '.summarise' but rather 'summarise'.  
Unfortunately there are no links in the help pages that seem to  
describe its proper use ... a not uncommon failing for that package in  
my experience.
>

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list