平均为2周和3周的间隔

时间:2016-08-12 12:50:09

标签: r dplyr lubridate

假设我有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

1 个答案:

答案 0 :(得分:0)

我们可以使用%/%

创建分组变量
n <- 2
my.df %>%
     group_by(Plot,  weekGrp = (weekofyear-1)%/% n + 1) %>%
     mutate(mean.temp = mean(Temperature))

更改&#39; n&#39;的值到3给出3个相邻的周年&#39;组合在一起。

注意:&#39; pH&#39; OP中的OP数据中未找到该列。

数据

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))