cast in reshape and reshape2

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

cast in reshape and reshape2

Kaiyin Zhong
> 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?
--
Kaiyin Zhong
------------------------------------------------------------------------------------------------------------------
Neuroscience Research Institute, Peking University, Beijing, P.R.China 100038

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

David Winsemius

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.

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

David Winsemius

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

Hadley Wickham-2
In reply to this post by Kaiyin Zhong
On Fri, Dec 23, 2011 at 1:58 PM, Kaiyin Zhong <[hidden email]> 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?

No, this is something that reshape2 does not support.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

Hadley Wickham-2
In reply to this post by David Winsemius
>> 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.

?plyr::summarise seems pretty helpful to me.  If you can do better,
please submit a patch - they are very much appreciated.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

David Winsemius

On Dec 23, 2011, at 9:23 PM, Hadley Wickham wrote:

>>> 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.
>
> ?plyr::summarise seems pretty helpful to me.  If you can do better,
> please submit a patch - they are very much appreciated.

My failure to find it stemmed from it not being mentioned in any way  
in package reshape2's help files, but maybe I was mistaken that it was  
meant to be used in that context?

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cast in reshape and reshape2

Hadley Wickham-2
>> ?plyr::summarise seems pretty helpful to me.  If you can do better,
>> please submit a patch - they are very much appreciated.
>
>
> My failure to find it stemmed from it not being mentioned in any way in
> package reshape2's help files, but maybe I was mistaken that it was meant to
> be used in that context?

Well, it's not - which is why it's not documented in reshape2.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.