gpt4 book ai didi

r - 使用each() 和reshape2::dcast 聚合数据

转载 作者:行者123 更新时间:2023-12-02 08:48:09 24 4
gpt4 key购买 nike

我通常使用 reshape 包来聚合一些数据(d'uh),通常与 plyr 一起使用,因为它的功能非常强大 each。最近,我收到建议切换到 reshape2 并尝试一下,但现在我似乎无法再使用 each 魔法了。

reshape

> m <- melt(mtcars, id.vars = c("am", "vs"), measure.vars = "hp")
> cast(m, am + vs ~ variable, each(min, max, mean, sd))
am vs hp_min hp_max hp_mean hp_sd
1 0 0 150 245 194.16667 33.35984
2 0 1 62 123 102.14286 20.93186
3 1 0 91 335 180.83333 98.81582
4 1 1 52 113 80.57143 24.14441

reshape 2

require(plyr)
> m <- melt(mtcars, id.vars = c("am", "vs"), measure.vars = "hp")
> dcast(m, am + vs ~ variable, each(min, max, mean, sd))
Error in structure(ordered, dim = ns) :
dims [product 4] do not match the length of object [16]
In addition: Warning messages:
1: In fs[[i]](x, ...) : no non-missing arguments to min; returning Inf
2: In fs[[i]](x, ...) : no non-missing arguments to max; returning -Inf

我没有心情对此进行梳理,因为我之前的代码就像 reshape 的魅力一样,但我真的很想知道:

  1. 是否可以将 eachdcast 一起使用?
  2. 是否建议使用reshape2reshape 已弃用吗?

最佳答案

您的第一个问题的答案似乎是。引用自 ?reshape2:::dcast:

If the combination of variables you supply does not uniquely identify one row in the original data set, you will need to supply an aggregating function, fun.aggregate. This function should take a vector of numbers and return a single summary statistic.

查看 Hadley 的 github 页面 reshape2表明他知道此功能已被删除,但似乎认为最好在 plyr 中完成,大概是这样的:

ddply(m,.(am,vs),summarise,min = min(value),
max = max(value),
mean = mean(value),
sd = sd(value))

或者如果您确实想继续使用each:

ddply(m,.(am,vs),function(x){each(min,max,mean,sd)(x$value)})

关于r - 使用each() 和reshape2::dcast 聚合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9659576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com