聚合需要基金。Aggregate:默认长度 [英] Aggregation requires fun.aggregate: length used as default
本文介绍了聚合需要基金。Aggregate:默认长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,我想对其进行重塑以使用R:这些是我正在运行的命令。
x <- data.frame(read.table("total.txt", sep=",", header=T)
y <- melt(x, id=c("Hostname", "Date", "MetricType"))
当我发出此命令以基本上将日期和小时组合在一起时,我收到一个错误,窗口挂起。
yy <- cast(y, Hostname + Date + variable ~ MetricType)
这是错误:
Aggregation requires fun.aggregate: length used as default
ServerNa Date MetricType Hour Value
19502 server1 01/05/2012 MemoryAVG Hour5 41.830000
19503 server1 01/05/2012 CPUMaximum Hour5 9.000000
19504 server1 01/05/2012 CPUAVG+Sev Hour5 9.060000
19505 server1 01/05/2012 CPUAVG Hour5 30.460000
19506 server1 01/05/2012 61 Hour5 63.400000
19507 server1 01/05/2012 60 Hour5 59.300000
19508 server2 01/05/2012 MemoryAVG Hour5 10.690000
19509 server2 01/05/2012 CPUMaximum Hour5 1.000000
19510 server2 01/05/2012 CPUAVG+Sev Hour5 0.080000
19511 server2 01/05/2012 CPUAVG Hour5 1.350000
是否有简单的方法可以在不挂起服务器的情况下完成此操作?
当我使用库(Rehape2)和这个:
yy <- acast(y, Hostname + Date + variable ~ MetricType, fun.aggregate=mean)
所有值都转换为NA。我不知道发生了什么事?
推荐答案
澄清:在下面的讨论中,我是指dcast()
而不是cast()
。正如慈母龙在评论中指出的那样,reshape
包中的函数cast()
在reshape2
包中被两个函数取代:dcast()
(用于data.Frame输出)和acast()
(用于数组或矩阵输出)。无论如何,我关于需要fun.aggregate
参数的评论同样适用于cast()
、dcast()
和acast()
。
之所以引发错误,是因为对于对
cast()
的调用中至少有一个类别变量组合,数据帧y
必须至少包含两行数据。如?cast
(或?dcast
)所述:
如果您提供的变量组合不是唯一的 标识原始数据集中的一行,您将需要提供 一个聚合函数,‘fun.asggregate’。运行下面的代码以查看这是如何工作的,以及如何补救它。在最后一行代码中,我使用
fun.aggregate
参数告诉dcast()
使用mean()
为任何重复的变量组合组合值。取而代之的是,您可以使用最适合您自己情况的任何聚合函数。
library(reshape2)
## A toy dataset, with one row for each combination of variables
d <- expand.grid(Hostname = letters[1:2],
Date = Sys.Date() + 0:1,
MetricType = LETTERS[3:4])
d$Value <- rnorm(seq_len(nrow(d)))
## A second dataset, in which one combination of variables is repeated
d2 <- rbind(d, d[1,])
## Runs without complaint
dcast(d, Hostname + Date ~ MetricType)
## Throws error asking for an aggregation function
dcast(d2, Hostname + Date ~ MetricType)
## Happy again, with a supplied aggregation function
dcast(d2, Hostname + Date ~ MetricType, fun.aggregate=mean)
这篇关于聚合需要基金。Aggregate:默认长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文