将数据从矩阵重新调整为跨国格式

时间:2014-12-09 11:02:09

标签: r dataframe reshape

我有一个具有以下形式的数据框(我的数据跨越12个月):

df <- data.frame(January = c(10,11,12,99,100,101), February = c(13,14,15,23,34,56), March = c(16,17,18,76,87,33),April = c(130,141,152,969,1010,1201), May = c(113,114,115,213,314,561), June = c(162,172,182,762,872,332))  
rownames(df) <- c("Males","Females","0-24","25-44","45-64","65+")

这就是数据的样子:

        January February March April May June  
Males        10       13    16   130 113  162  
Females      11       14    17   141 114  172  
0-24         12       15    18   152 115  182  
25-44        99       23    76   969 213  762  
45-64       100       34    87  1010 314  872  
65+         101       56    33  1201 561  332  

我想对其进行重新组织,以便更好地分析并绘制图表。理想的格式如下:

Month, Sex (rows 1 and 2 of each column), AgeGroup (rows 3 - 6 of each column).

我该怎么办呢?

发表评论后,这里给出的数字已经完成。实际数据来自http://www.opendatamalta.org/ckan/dataset/50cfd96f-246e-4d57-a0ce-86c8ce88e9d6/resource/fad12a26-2965-404f-9320-c0bc82353315/download/Profileofdepartingtourists2012.csv

IMO,理想的格式是两个数据帧:

dfSex

Month    -   Sex.Factor  -  Sex.Value
January  -   M           -  10
January  -   F           -  11
February -   M           -  13 
February -   F           -  14
March    -   M           -  16 
March    -   F           -  17
**etc**

dfAgeG

Month     -  AgeG.Factor  - AgeG.Value
January  -   0-24         -  12
January  -   25-44        -  99
January  -   45-64        -  100 
January  -   65+          -  101
February -   0-24         -  15
February -   25-44        -  23
February -   45-64        -  34 
February -   65+          -  101
March    -   0-24         -  18
March    -   25-44        -  76
March    -   45-64        -  87 
March    -   65+          -  33
**etc**

1 个答案:

答案 0 :(得分:1)

如答案中的评论所示,来自“reshape2”的melt对于此问题应该足够(并且非常直接)。它实际上也适用于t(df),因为它调用matrix的{​​{1}}方法,它使用melt在结果rownames中创建变量。

这是方法:

data.frame