我通常使用reshape包来聚合一些数据(呃),通常用plyr,因为每个都有超级棒的功能.最近,我收到了一个建议,切换到reshape2并尝试一下,现在我似乎无法再使用每个魔法.
重塑
> 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
reshape2
require(plyr) > m <- melt(mtcars,measure.vars = "hp") > dcast(m,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
我没有心情去梳理它,因为我之前的代码就像一个重塑的魅力,但我真的很想知道:
>是否有可能与dcast一起使用?
>是否建议使用reshape2?重塑已弃用?
解决方法
你的第一个问题的答案似乎是否定的.引自?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的reshape2的github页面表明他知道这个功能被删除了,但似乎认为在plyr中做得更好,大概是这样的:
ddply(m,.(am,vs),summarise,min = min(value),max = max(value),mean = mean(value),sd = sd(value))
或者如果你真的想继续使用每个:
ddply(m,function(x){each(min,sd)(x$value)})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。