R:对于若干年的时间跨度,计算每个日历月测量值的平均值
我得到了一个R:对于若干年的时间跨度,计算每个日历月测量值的平均值,r,date,R,Date,我得到了一个data.frame,就像我找到的最后一个(df)。我想计算每个月的平均值,例如,df$PH。我胡闹了很长一段时间,最后(在他的帮助下)得到了以下结果: 你知道吗? (否则人们至少可以找到这个。) df如果您只想计算每个月给定列的平均值,您可以尝试使用,我认为这会更容易 library(data.table) df <- as.data.table(df) df[, .(meanByMonth = mean(PH)), by = month(TIME)][order(mont
data.frame
,就像我找到的最后一个(df)。我想计算每个月的平均值,例如,df$PH
。我胡闹了很长一段时间,最后(在他的帮助下)得到了以下结果:
你知道吗?(否则人们至少可以找到这个。)
df如果您只想计算每个月给定列的平均值,您可以尝试使用,我认为这会更容易
library(data.table)
df <- as.data.table(df)
df[, .(meanByMonth = mean(PH)), by = month(TIME)][order(month)]
month meanByMonth
1: 1 7.221429
2: 2 7.285000
3: 3 7.284167
4: 4 7.428571
5: 5 7.255000
6: 6 7.252632
7: 7 7.220000
8: 8 7.134444
9: 9 7.299167
10: 10 7.102500
11: 11 7.048214
12: 12 7.250000
库(data.table)
df如果您只想计算每个月给定列的平均值,您可以尝试使用,我认为这会更容易
library(data.table)
df <- as.data.table(df)
df[, .(meanByMonth = mean(PH)), by = month(TIME)][order(month)]
month meanByMonth
1: 1 7.221429
2: 2 7.285000
3: 3 7.284167
4: 4 7.428571
5: 5 7.255000
6: 6 7.252632
7: 7 7.220000
8: 8 7.134444
9: 9 7.299167
10: 10 7.102500
11: 11 7.048214
12: 12 7.250000
库(data.table)
df您可以在base R中使用聚合
,并使用一些日期格式“magic”来计算
对于每年的每个月,您都可以使用:
aggregate(PH~format(TIME, "%Y-%m"), data=df, FUN=mean)
返回
format(TIME, "%Y-%m") PH
1 2000-07 7.220000
2 2000-12 7.070000
3 2001-01 7.020000
4 2001-08 7.045000
5 2001-11 6.880000
6 2002-03 6.960000
7 2002-07 7.220000
...
31 2010-05 7.207143
32 2010-11 7.096667
33 2013-04 7.528571
34 2014-03 7.268333
format(TIME, "%m") PH
1 01 7.221429
2 02 7.285000
3 03 7.284167
4 04 7.428571
5 05 7.255000
6 06 7.252632
7 07 7.220000
8 08 7.134444
9 09 7.299167
10 10 7.102500
11 11 7.048214
12 12 7.250000
对于每个月,跨越几年,您可以使用
aggregate(PH~format(TIME, "%m"), data=df, FUN=mean)
返回
format(TIME, "%Y-%m") PH
1 2000-07 7.220000
2 2000-12 7.070000
3 2001-01 7.020000
4 2001-08 7.045000
5 2001-11 6.880000
6 2002-03 6.960000
7 2002-07 7.220000
...
31 2010-05 7.207143
32 2010-11 7.096667
33 2013-04 7.528571
34 2014-03 7.268333
format(TIME, "%m") PH
1 01 7.221429
2 02 7.285000
3 03 7.284167
4 04 7.428571
5 05 7.255000
6 06 7.252632
7 07 7.220000
8 08 7.134444
9 09 7.299167
10 10 7.102500
11 11 7.048214
12 12 7.250000
由于列名可能不是您想要的,您可以在setNames
中包装aggregate
,并提供更好的列名,或者在下一行中这样做
setNames(aggregate(PH~format(TIME, "%m"), data=df, FUN=mean), c("months", "meanPH"))
通过将cbind
包含到aggregate
中,可以轻松添加更多列:
aggregate(cbind(PH, EH)~format(TIME, "%m"), data=df, FUN=mean)
或者,您可以使用标准接口来聚合
,其中第二个参数中的列表确定分组
aggregate(df[c("PH", "EH")], list(format(df$TIME, "%m")), FUN=mean)
您可以在base R中使用aggregate
,并使用一点日期格式“magic”来计算
对于每年的每个月,您都可以使用:
aggregate(PH~format(TIME, "%Y-%m"), data=df, FUN=mean)
返回
format(TIME, "%Y-%m") PH
1 2000-07 7.220000
2 2000-12 7.070000
3 2001-01 7.020000
4 2001-08 7.045000
5 2001-11 6.880000
6 2002-03 6.960000
7 2002-07 7.220000
...
31 2010-05 7.207143
32 2010-11 7.096667
33 2013-04 7.528571
34 2014-03 7.268333
format(TIME, "%m") PH
1 01 7.221429
2 02 7.285000
3 03 7.284167
4 04 7.428571
5 05 7.255000
6 06 7.252632
7 07 7.220000
8 08 7.134444
9 09 7.299167
10 10 7.102500
11 11 7.048214
12 12 7.250000
对于每个月,跨越几年,您可以使用
aggregate(PH~format(TIME, "%m"), data=df, FUN=mean)
返回
format(TIME, "%Y-%m") PH
1 2000-07 7.220000
2 2000-12 7.070000
3 2001-01 7.020000
4 2001-08 7.045000
5 2001-11 6.880000
6 2002-03 6.960000
7 2002-07 7.220000
...
31 2010-05 7.207143
32 2010-11 7.096667
33 2013-04 7.528571
34 2014-03 7.268333
format(TIME, "%m") PH
1 01 7.221429
2 02 7.285000
3 03 7.284167
4 04 7.428571
5 05 7.255000
6 06 7.252632
7 07 7.220000
8 08 7.134444
9 09 7.299167
10 10 7.102500
11 11 7.048214
12 12 7.250000
由于列名可能不是您想要的,您可以在setNames
中包装aggregate
,并提供更好的列名,或者在下一行中这样做
setNames(aggregate(PH~format(TIME, "%m"), data=df, FUN=mean), c("months", "meanPH"))
通过将cbind
包含到aggregate
中,可以轻松添加更多列:
aggregate(cbind(PH, EH)~format(TIME, "%m"), data=df, FUN=mean)
或者,您可以使用标准接口来聚合
,其中第二个参数中的列表确定分组
aggregate(df[c("PH", "EH")], list(format(df$TIME, "%m")), FUN=mean)
data.table
能做的事情真是太神奇了!我必须尽快研究它!是的,是的。我用一张备忘单编辑了答案,我发现这张备忘单在处理data.tables时特别有用。我希望它能帮上忙。data.table
能做的事情真是太神奇了!我必须尽快研究它!是的,是的。我用一张备忘单编辑了答案,我发现这张备忘单在处理data.tables时特别有用。我希望它能有所帮助。有没有一种方法可以让聚合返回一个数据。框架和pH平均值以及EH平均值(示例数据中的另一列)?否则,通过cbind
就很清楚了:cbind(聚合(pH~format(TIME),%Y-%m),data=df,FUN=mean),(聚合(EH~format(TIME,%Y-%m),data=df,FUN=mean)[,2])
。请参阅底部的“我的编辑”。它使用公式界面将cbind
合并到aggregate
函数中,以aggregate
或显示公式界面的替代方法。是否有办法让aggregate返回带有pH均值和EH均值的数据。frame
(示例数据中的另一列)?否则很清楚如何通过cbind
:cbind(聚合(PH~format(TIME),%Y-%m),data=df,FUN=mean),(聚合(EH~format(TIME,%Y-%m),data=df,FUN=mean)[,2])
。请参见底部的“我的编辑”。它使用公式界面将cbind
合并到aggregate
函数中,以aggregate
或显示公式界面的替代方案。