【问题标题】:difference between ddply and aggregateddply 和聚合之间的区别
【发布时间】:2013-01-05 21:47:58
【问题描述】:

有人可以通过以下示例帮助我了解聚合和 ddply 之间的区别:

一个数据框:

mydat <- data.frame(first = rpois(10,10), second = rpois(10,10), 
                    third = rpois(10,10), group = c(rep("a",5),rep("b",5)))

使用聚合将函数应用于按因子分割的数据框的一部分:

aggregate(mydat[,1:3], by=list(mydat$group), mean)
  Group.1 first second third
1       a   8.8    8.8  10.2
2       b   6.8    9.4  13.4

尝试对另一个函数使用聚合(返回错误消息):

aggregate(mydat[,1:3], by=list(mydat$group), function(u) cor(u$first,u$second))
Error in u$second : $ operator is invalid for atomic vectors

现在,用 ddply(plyr 包)试试同样的方法:

ddply(mydat, .(group), function(u) cor(u$first,u$second))
  group         V1
1     a -0.5083042
2     b -0.6329968

高度赞赏所有提示、链接和批评。

【问题讨论】:

  • 我认为您已经证明了不同之处。还是这里有问题?
  • 好吧,虽然我看到有区别,但我不明白为什么会这样。这些函数中的什么导致了我展示的差异?
  • programming-r-pro-bro.blogspot.com/2012/12/… 的第 5 部分通过示例代码进行了精彩的解释。基本上,ddply 将允许您对每个变量使用不同的函数,而聚合则强制您对传递的所有列使用相同的函数。

标签: r


【解决方案1】:

aggregate 在每一列上独立调用 FUN,这就是你获得独立手段的原因。 ddply 会将所有列传递给函数。快速演示 aggregate 中传递的内容可能是有序的:

一些示例数据用于演示:

d <- data.frame(a=1:4, b=5:8, c=c(1,1,2,2))

> d
  a b c
1 1 5 1
2 2 6 1
3 3 7 2
4 4 8 2

通过使用函数print 并忽略命令aggregateddply 的结果,我们可以看到在每次迭代中传递给函数的内容。

aggregate:

tmp <- aggregate(d[1:2], by=list(d$c), print)
[1] 1 2
[1] 3 4
[1] 5 6
[1] 7 8

请注意,单独的列被发送到打印。

ddply:

tmp <- ddply(d, .(c), print)
  a b c
1 1 5 1
2 2 6 1
  a b c
3 3 7 2
4 4 8 2

请注意,正在发送数据帧以进行打印。

【讨论】:

  • 酷,谢谢:前两句正是我要找的内容。
  • @Arun 我粘贴的是printaggregate 中的输出。我省略了 aggregate 的结果,这对于本示例并不重要。我将编辑答案以更好地表明这一点。
【解决方案2】:

您已经被告知为什么 aggregate 是错误的 {base} 函数,用于需要两个向量作为参数的函数,但您尚未被告知哪种非 ddply 方法会成功。

by( ... grp, FUN) 方法:

> cbind (by( mydat, mydat["group"], function(d) cor(d$first, d$second)) )
        [,1]
a  0.6529822
b -0.1964186

sapply(split( ..., grp), fn) 方法

> sapply(  split( mydat, mydat["group"]), function(d) cor(d$first, d$second)) 
         a          b 
 0.6529822 -0.1964186 

【讨论】:

  • 当我求助于plyr/ddply 时,我总是感到内疚,因为我的意思是我失败了,或者太不耐烦地理解聚合、by 或 tapply。但是请注意,ddply 非常笼统,对于大问题可能会很慢。 ddply 的快速版本正在这里增长:github.com/hadley/dplyr
【解决方案3】:

@MatthewLundberg 的答案非常好,我的答案不是答案,但它只是我想查看某些 R 函数调用背后发生的情况时使用的一般提示。我使用调试命令browser

aggregate(mydat[,1:3], by=list(mydat$group), 
+           function(x){
+             browser()
+             mean(x)
+           })
Called from: FUN(X[[1L]], ...)
Browse[1]> x
[1] 16 10 16 13 25

那么对于ddply

ddply(mydat, .(group), function(u) {
+   browser()
+   cor(u$first,u$second)
+   })
Called from: .fun(piece, ...)
Browse[1]> u
  first second third group
1    16      8     9     a
2    10      6     6     a
3    16      6    10     a
4    13      8    10     a
5    25     10     4     a

自行编辑调试错误

这里我用这个技巧来看看你为什么会出错

aggregate(mydat[,1:3], by=list(mydat$group), function(u) {
+   browser()
+   cor(u$first,u$second)
+   })
Called from: FUN(X[[1L]], ...)
Browse[1]> u
[1] 16 10 16 13 25    

正如你在这里看到的,你是一个原子向量(没有列名) 所以如果你尝试

Browse[1]> u$first

你得到一个错误:

Error in u$first : $ operator is invalid for atomic vectors

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 2011-03-06
    • 1970-01-01
    • 2011-12-20
    • 2017-12-05
    • 2015-06-12
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多