[英]How to create a table from the aggregate() function in R
I have a data set that looks like this: 我有一个如下所示的数据集:
Type Date Lively Count
sm 1/13/2010 10 10
sm 1/14/2010 10 20
sm 2/15/2010 20 30
am 4/16/2010 5 42
am 1/17/2010 10 34
am 3/18/2010 40 54
sm 1/19/2010 10 65
sm 4/20/2010 5 67
sm 3/21/2010 40 76
I'm trying to average out all the numeric parameters by month. 我试图按月平均所有数字参数。 So my resultant data set would ideally be:
所以我的结果数据集理想情况是:
Date Lively Count
Jan 2010 10 32.25
Feb 2010 20 30.00
Mar 2010 40 65.00
Apr 2010 5 54.50
I'm very close to this, what I currently have is: 我非常接近这个,我现在拥有的是:
Lively Count
Jan 2010 10 32.25
Feb 2010 20 30.00
Mar 2010 40 65.00
Apr 2010 5 54.50
As you can see I'm missing the title 'Date'. 如你所见,我错过了标题'日期'。 Here is my code:
这是我的代码:
library(zoo)
z <- zoo(data[3:4], as.Date(data[,2], "%m/%d/%Y"))
aggregate(z, as.yearmon, mean)
I don't know how to make a title for the left column ('Date'), and more importantly, I don't know how to make the output of aggregate() into a table (resultant data set). 我不知道如何为左列('Date')创建标题,更重要的是,我不知道如何将aggregate()的输出变成表(结果数据集)。
You can use the list
format to specify your names within aggregate
. 您可以使用
list
格式在aggregate
指定您的名称。
To get your "date" values, you need to refer to the "index" of your zoo object 要获取“日期”值,需要引用zoo对象的“索引”
aggregate(list(Lively = z[, "Lively"], Count = z[, "Count"]),
list(Date = as.yearmon(index(z))), mean)
# Date Lively Count
# 1 Jan 2010 10 32.25
# 2 Feb 2010 20 30.00
# 3 Mar 2010 40 65.00
# 4 Apr 2010 5 54.50
Alternatively, you can easily change your names if required. 或者,您可以根据需要轻松更改名称。 This will allow you to be able to use the much nicer formula method for
aggregate
. 这将使您能够使用更好的公式方法进行
aggregate
。
x <- aggregate(. ~ as.yearmon(index(z)), z, mean)
names(x)[1] <- "Date"
x
# Date Lively Count
# 1 Jan 2010 10 32.25
# 2 Feb 2010 20 30.00
# 3 Mar 2010 40 65.00
# 4 Apr 2010 5 54.50
Note, however, that by doing so, you miss out on all the goodness that zoo
has to offer by doing this. 但请注意,通过这样做,你会错过
zoo
提供的所有优点。 You've essentially taken a data.frame
, converted it to a zoo
object, and re-converted it to a data.frame
. 您基本上采用了
data.frame
,将其转换为zoo
对象,并将其重新转换为data.frame
。
Your aggregate(z, as.yearmon, mean)
solution would be the way I would go about it, and refer to the date by using index()
. 您的
aggregate(z, as.yearmon, mean)
解决方案将是我的方式,并使用index()
引用日期。
If you're just doing this at a later stage for aesthetic reasons, you can keep working with zoo
objects since it will give you a lot of flexibility that you might not get with base R functions, and then use cbind
at the end. 如果你出于审美原因只是在稍后阶段这样做,你可以继续使用
zoo
对象,因为它将为你提供很多灵活性,你可能无法使用基本R函数,然后在最后使用cbind
。
Following from where you left off: 从你离开的地方开始:
library(zoo)
z <- zoo(data[3:4], as.Date(data[,2], "%m/%d/%Y"))
x <- aggregate(z, as.yearmon, mean)
cbind(Date = index(x),
as.data.frame.matrix(x, row.names = NULL))
# Date Lively Count
# 1 Jan 2010 10 32.25
# 5 Feb 2010 20 30.00
# 6 Mar 2010 40 65.00
# 8 Apr 2010 5 54.50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.