转置/重塑数据格式

时间:2019-05-13 14:11:59

标签: r transform reshape transpose

我该如何在R中转换数据集?

数据现在具有以下格式:

cityname   unit   2005   2006   2007

Alabama    pop    -      -      - 
Alabama    inc    -      -      -
Alabama    pop/inc-      -      -
Alaska     pop   -       -      -
Alaska     inc    -      -      -
Alaska     pop/inc -     -       -
California pop     -    -       -
California inc     -     -     -
California pop/inc  -    -      -

我想要这个:

cityname  pop    inc    pop/inc  year

Alabama    -      -      -      2005
Alaska    -      -      -      2005
California    -      -    -    2005
Alabama   -       -      -     2006
Alaska      -      -      -    2006
California  -      -      -    2006
Alabama    -     -       -     2007
Alaska    -     -     -        2007
California   -    -      -     2007 

我尝试过这样做,但是顺序却不正确

IncPopdata <- t(MAINC1_ALL_AREAS_1969_2017)

enter image description here

0 个答案:

没有答案