【问题标题】:Proper way to split data frames at multiple levels using ddply [duplicate]使用 ddply 在多个级别拆分数据帧的正确方法 [重复]
【发布时间】:2016-09-27 12:18:23
【问题描述】:

假设我有一个如下所示的数据框:

  year stint  ID  W
1 2003     1 abc 10
2 2003     2 abc  3
3 2003     1 def 16
4 2004     1 abc 15
5 2004     1 def 11
6 2004     2 def  7

我想合并数据,使其看起来像

  year  ID  W
1 2003 abc 13
3 2003 def 16
4 2004 abc 15
5 2004 def 18

我找到了一种可以根据需要组合数据的方法,但我很确定还有更好的方法。

combinedData = unique(ddply(data, "ID", function(x) {
    ddply(x, "year", function(y) {
        data.frame(ID=x$ID, W=sum(y$W))
    })
}))
combinedData[order(combinedData$year),]

这会产生以下输出:

   year  ID  W
1  2003 abc 13
7  2003 def 16
4  2004 abc 15
10 2004 def 18

具体来说,我不喜欢我必须使用唯一的(否则我会在输出的数据中获得 3 次 year、ID、W 的每个唯一组合),并且我不喜欢行号不是连续的.我怎样才能更干净地做到这一点?

【问题讨论】:

  • 试试dplyr,即data %>% group_by(year, ID) %>% summarise(W= sum(W))。如果您使用的是ddply,那么ddply(data, .(year, ID), summarise, W=sum(W))

标签: r dataframe plyr


【解决方案1】:

使用基础 R 执行此操作:

aggregate(W~year+ID, df, sum)

#  year  ID  W
#1 2003 abc 13
#2 2004 abc 15
#3 2003 def 16
#4 2004 def 18

数据

df <- structure(list(year = c(2003L, 2003L, 2003L, 2004L, 2004L, 2004L
), stint = c(1L, 2L, 1L, 1L, 1L, 2L), ID = structure(c(1L, 1L, 
2L, 1L, 2L, 2L), .Label = c("abc", "def"), class = "factor"), 
    W = c(10L, 3L, 16L, 15L, 11L, 7L)), .Names = c("year", "stint", 
"ID", "W"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2013-11-16
    相关资源
    最近更新 更多