[R] Help with package reshape

Murray Jorgensen maj at stats.waikato.ac.nz
Sun Oct 25 10:07:28 CET 2009


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

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




More information about the R-help mailing list