【问题标题】:Reshaping data into panel format in R在R中将数据重塑为面板格式
【发布时间】:2010-12-30 22:47:12
【问题描述】:

我有一个相当长且(对我而言)复杂的问题。我有来自欧盟理事会的投票数据,其中每个国家的投票行为都根据名义规模进行了编码:

0: yes
1: no
2: Abstention
3: no with a negative statement
4: Abstention with a negative statement
5: yes with a negative statement

数据采用以下格式(请参阅帖子末尾的数据集中 20 个观察值的转储):

Country1 Country2 Country3 ... Date
1        0        0        ... 2004-12-12
1        2        0        ... 2003-02-14
2        0        1        ... 2004-05-22
...      ...      ...      ... ...

首先,我想将数据汇总为每月间隔,每个月我们都有每个国家/地区有多少个 0、1、2 等的总和。理想情况下,数据应如下所示:

Month    Country   sum of 0s    sum of 1s   Sum of 2s
January  Country1  2            0           1  
January  Country2  4            0           0
...      ...       ...          ...         ...

完成此操作后,我想将数据放入面板格式,如下所示:

Country   Month    sum of 0s   sum of 1s   sum of 2s
Country1  January  2           0           1 
Country1  February 0           1           3
...       ...      ...         ...         ...
Country2  January  4           0           0
Country2  February 2           2           0
...       ...      ...         ...         ...

如果这是一个非常耗时的问题,我很抱歉,但我一直在玩聚合、不同的应用函数,但无法获得预期的结果。任何帮助将不胜感激!

来自数据集的 20 个观察值(来自 dput() 函数的输出):

    structure(list(Recitals = c(29L, 13L, 2L, 20L, 10L, 18L, 29L, 
6L, 4L, 16L, 7L, 6L, 12L, 23L, 6L, 10L, 2L, 6L, 9L, 8L, 7L), 
    Voting_Rule = structure(c(4L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 
    4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 4L), .Label = c("0", 
    "Qualified Majority", "Simple Majority", "Unanimity", "Qualified majority", 
    "Simple majority"), class = "factor"), Belgium = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), Denmark = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Czech.Republic = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Germany = c(0L, 
    0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Estonia = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Greece = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Spain = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), France = c(0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Ireland = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Italy = c(0L, 0L, 0L, 0L, 0L, 0L, 5L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Cyprus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Latvia = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), Lithuania = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Luxembourg = c(0L, 
    0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Hungary = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Malta = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Netherlands = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Austria = c(0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Poland = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Portugal = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Slovenia = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Slovakia = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Finland = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Sweden = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    UK = c(0L, 0L, 0L, 0L, 0L, 0L, 5L, 0L, 0L, 0L, 5L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Dates = structure(c(12716, 
    12716, 12716, 12674, 12674, 12698, 12705, 12724, 12738, 12738, 
    12716, 12741, 12744, 12754, 12754, 12758, 12758, 12758, 12759, 
    12759, 12759), class = "Date")), .Names = c("Recitals", "Voting_Rule", 
"Belgium", "Denmark", "Czech.Republic", "Germany", "Estonia", 
"Greece", "Spain", "France", "Ireland", "Italy", "Cyprus", "Latvia", 
"Lithuania", "Luxembourg", "Hungary", "Malta", "Netherlands", 
"Austria", "Poland", "Portugal", "Slovenia", "Slovakia", "Finland", 
"Sweden", "UK", "Dates"), row.names = c(752L, 753L, 762L, 774L, 
775L, 776L, 777L, 780L, 789L, 790L, 793L, 794L, 797L, 816L, 817L, 
818L, 819L, 820L, 824L, 825L, 826L), class = "data.frame")

【问题讨论】:

    标签: r dataset panel aggregate reshape


    【解决方案1】:

    Hadley 的 reshape2 和 plyr 包将极大地帮助您的工作:

    library(reshape2)
    
    # load your data into variable d1
    # d1 <- struct( ... your data ...)
    
    # first, melt the data.
    # molten data is very very useful for further transformation.
    d2 <- reshape2:::melt.data.frame(d1[,-(1:2)], # drop unused variables
                                     id.vars="Dates",
                                     variable.name="Country")
    
    # create a variable "Month" from Date
    d2$Month <- months(d2$Date)
    
    # cast the data to the desired format using appropriate function (here, length())
    d3 <- dcast(d2, Country+Month~value, length)
    
    # finally change the names of newly created variables
    names(d3)[-(1:2)] <- sprintf("sum of %ss", names(d3)[-(1:2)])
    

    【讨论】:

    • 感谢您的快速回复,代码正常工作,我只有一个问题:使用 reshape2 包似乎按字母顺序排列月份。是否可以按时间顺序排列(即 1 月、2 月等)?
    • Reshape 不知道月份的顺序 - 您需要通过使用具有正确顺序级别的因子来告诉 R。另请参阅 count 以获得计算 d3 的快速替代方案。
    • 谢谢,是的,count() 可用。但是如果我的理解是正确的,count() 可能会产生“长”格式并且不能产生“宽”格式,因此在这种情况下 count() 之后需要进行另一个转换。
    • 感谢您的帮助,这为我节省了很多时间!
    猜你喜欢
    • 2012-02-18
    • 2021-08-27
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 2020-10-23
    • 2020-10-01
    • 2017-07-20
    相关资源
    最近更新 更多