[R] Help with package reshape

David Winsemius dwinsemius at comcast.net
Sun Oct 25 14:12:23 CET 2009


On Oct 25, 2009, at 5:07 AM, Murray Jorgensen wrote:

> I have got part of the way to what I want by playing with a small  
> example:
>
> > example2
>   ano census total.pop class
> 1  222     96      1131    11
> 2  222      1      1245    12
> 3  239     96      3921    11
> 4  239      1      4503    12
> 5  260      1      4218    11
> 6  269      1      1185    12
> 7  275      1      3555    11
> 8  308     96       948    12
> 9  349     96      2511    11
> 10 349      1      2808    12
> > mexa2 = melt(example2, id = c("ano","census"))
> > mexa2
>   ano census  variable value
> 1  222     96 total.pop  1131
> 2  222      1 total.pop  1245
> 3  239     96 total.pop  3921
> 4  239      1 total.pop  4503
> 5  260      1 total.pop  4218
> 6  269      1 total.pop  1185
> 7  275      1 total.pop  3555
> 8  308     96 total.pop   948
> 9  349     96 total.pop  2511
> 10 349      1 total.pop  2808
> 11 222     96     class    11
> 12 222      1     class    12
> 13 239     96     class    11
> 14 239      1     class    12
> 15 260      1     class    11
> 16 269      1     class    12
> 17 275      1     class    11
> 18 308     96     class    12
> 19 349     96     class    11
> 20 349      1     class    12
> > cast(mexa2, ... ~ census)
>   ano  variable    1   96
> 1  222 total.pop 1245 1131
> 2  222     class   12   11
> 3  239 total.pop 4503 3921
> 4  239     class   12   11
> 5  260 total.pop 4218   NA
> 6  260     class   11   NA
> 7  269 total.pop 1185   NA
> 8  269     class   12   NA
> 9  275 total.pop 3555   NA
> 10 275     class   11   NA
> 11 308 total.pop   NA  948
> 12 308     class   NA   12
> 13 349 total.pop 2808 2511
> 14 349     class   12   11
> >
>
> This is nearly what I want but I really want something like this:
>
>   ano  total.pop1 total.pop96  class1  class96
>   222        1245        1131      12       11
>   239        4503        3921      12       11
>   260        4218          NA      11       NA
>   269        1185          NA      12       NA
>   275        3555          NA      11       NA
>   308          NA         948      NA       12
>   349        2808        2511      12       11

library(reshape)

mexa2 = melt(example2, id = c("ano","census"))
example2 <- read.table(textConnection("ano census total.pop class
1  222     96      1131    11
2  222      1      1245    12
3  239     96      3921    11
4  239      1      4503    12
5  260      1      4218    11
6  269      1      1185    12
7  275      1      3555    11
8  308     96       948    12
9  349     96      2511    11
10 349      1      2808    12"), header=T)

cast(mexa2, ano ~ variable+census)
   ano total.pop_1 total.pop_96 class_1 class_96
1 222        1245         1131      12       11
2 239        4503         3921      12       11
3 260        4218           NA      11       NA
4 269        1185           NA      12       NA
5 275        3555           NA      11       NA
6 308          NA          948      NA       12
7 349        2808         2511      12       11

or
cast(mexa2, ... ~ variable+census)

-- 
David

>
> Suggestions gratefully received!
>
> Regards,  Murray Jorgensen
>
> -- 
> Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
> Department of Statistics, University of Waikato, Hamilton, New Zealand
> Email: maj at waikato.ac.nz    majorgensen at ihug.co.nz      Fax 7 838 4155
> Phone  +64 7 838 4773 wk    Home +64 7 825 0441   Mobile 021 0200 8350
>
> ______________________________________________
> R-help at r-project.org 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list