以 2 周和 3 周为间隔的平均值
Average at 2 week and 3 week intervals
假设我有 4 块地,我每小时测量每块地的温度、湿度、土壤 pH 值等。我可以通过 dplyr
计算每周平均值
library(dplyr)
my.dfA = group_by(my.df, plot, weeknumber)
my.dfB = mutate(my.dfA, mean.temp = mean(temp), mean.pH = mean(pH))
如何获取两周平均值,然后是每个地块的三周平均值,等等?我不想要移动平均线;我想将第 13 周和第 14 周折叠成一个两周的周期,然后对第 15 周和第 16 周做同样的事情,依此类推。
# A tibble: 6 × 5
Date Temperature Plot dayofyear weekofyear
<dttm> <dbl> <dbl> <dbl> <dbl>
1 2016-04-03 15:24:00 13.0 1 94 14
2 2016-04-03 15:39:00 13.0 1 94 14
3 2016-04-03 15:53:59 13.0 1 94 14
4 2016-04-03 16:09:00 13.5 1 94 14
5 2016-04-03 16:24:00 13.0 1 94 14
6 2016-04-03 16:38:59 13.0 1 94 14
我们可以使用 %/%
创建分组变量
n <- 2
my.df %>%
group_by(Plot, weekGrp = (weekofyear-1)%/% n + 1) %>%
mutate(mean.temp = mean(Temperature))
将 'n' 的值更改为 3 会将 3 个相邻的 'weekofyear' 组合在一起。
注意:在 post 的 OP 数据中找不到 'pH' 列。
数据
my.df <- structure(list(Date = c("2016-04-03 15:24:00", "2016-04-03 15:39:00",
"2016-04-03 15:53:59", "2016-04-03 16:09:00", "2016-04-03 16:24:00",
"2016-04-03 16:38:59", "2016-04-03 15:24:00", "2016-04-03 15:39:00",
"2016-04-03 15:53:59", "2016-04-03 16:09:00", "2016-04-03 16:24:00",
"2016-04-03 16:38:59"), Temperature = c(13, 13, 12, 14.5, 13,
13, 14, 13, 16, 13.5, 18, 19), Plot = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), dayofyear = c(94L, 94L, 94L, 94L, 94L,
94L, 94L, 94L, 94L, 94L, 94L, 94L), weekofyear = c(13L, 13L,
13L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L)), .Names = c("Date",
"Temperature", "Plot", "dayofyear", "weekofyear"), class = "data.frame",
row.names = c(NA, -12L))
假设我有 4 块地,我每小时测量每块地的温度、湿度、土壤 pH 值等。我可以通过 dplyr
计算每周平均值 library(dplyr)
my.dfA = group_by(my.df, plot, weeknumber)
my.dfB = mutate(my.dfA, mean.temp = mean(temp), mean.pH = mean(pH))
如何获取两周平均值,然后是每个地块的三周平均值,等等?我不想要移动平均线;我想将第 13 周和第 14 周折叠成一个两周的周期,然后对第 15 周和第 16 周做同样的事情,依此类推。
# A tibble: 6 × 5
Date Temperature Plot dayofyear weekofyear
<dttm> <dbl> <dbl> <dbl> <dbl>
1 2016-04-03 15:24:00 13.0 1 94 14
2 2016-04-03 15:39:00 13.0 1 94 14
3 2016-04-03 15:53:59 13.0 1 94 14
4 2016-04-03 16:09:00 13.5 1 94 14
5 2016-04-03 16:24:00 13.0 1 94 14
6 2016-04-03 16:38:59 13.0 1 94 14
我们可以使用 %/%
n <- 2
my.df %>%
group_by(Plot, weekGrp = (weekofyear-1)%/% n + 1) %>%
mutate(mean.temp = mean(Temperature))
将 'n' 的值更改为 3 会将 3 个相邻的 'weekofyear' 组合在一起。
注意:在 post 的 OP 数据中找不到 'pH' 列。
数据
my.df <- structure(list(Date = c("2016-04-03 15:24:00", "2016-04-03 15:39:00",
"2016-04-03 15:53:59", "2016-04-03 16:09:00", "2016-04-03 16:24:00",
"2016-04-03 16:38:59", "2016-04-03 15:24:00", "2016-04-03 15:39:00",
"2016-04-03 15:53:59", "2016-04-03 16:09:00", "2016-04-03 16:24:00",
"2016-04-03 16:38:59"), Temperature = c(13, 13, 12, 14.5, 13,
13, 14, 13, 16, 13.5, 18, 19), Plot = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), dayofyear = c(94L, 94L, 94L, 94L, 94L,
94L, 94L, 94L, 94L, 94L, 94L, 94L), weekofyear = c(13L, 13L,
13L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L)), .Names = c("Date",
"Temperature", "Plot", "dayofyear", "weekofyear"), class = "data.frame",
row.names = c(NA, -12L))