计算平均值,以R为间隔

时间:2019-02-25 11:19:20

标签: r

我找不到重复的问题,希望您能帮忙。

使用一个简单的示例,我希望基于指定的窗口大小(称为n)来计算列的平均值/平均值。

data <- data.frame(x = rep(1:10,1), y = rep(11:20, 1))

我希望添加一列z,该列一次计算四行的平均值。

结果将是:

structure(list(x = 1:10, y = 11:20, z = c("NA", "NA", "NA", "12.5", 
"13.5", "14.5", "15.5", "16.5", "17.5", "18.5")), class = "data.frame", .Names = c("x", 
"y", "z"), row.names = c(NA, -10L))

我计算出的平均行平均为一列,间隔为n行,如下所示:

#For n = 4, row 4 is calculated as (11+12+13+14)/n
#For n =4, row 5 is calculated as (12+13+14+15)/n
#And so on ...

我查看了以下帖子,例如:

  1. how to calculate combined column mean value in R
  2. Calculate mean by group
  3. How to calculate average of a variable by hour in R
  4. Calculate the mean of every 13 rows in data frame
  5. calculate a mean by criteria in R

我在下面尝试了此代码,但无法获得写解决方案。

data<-data %>% mutate(z=rollapplyr(y,10,FUN=mean,by=4))

感谢您的帮助。谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用图书馆动物园的滚动平均值来实现

data <- data.frame(x = rep(1:10,1), y = rep(11:20, 1))

result <- structure(list(x = 1:10, y = 11:20, z = c("NA", "NA", "NA", "12.5", 
                                                    "13.5", "14.5", "15.5", "16.5", "17.5", "18.5")), class = "data.frame", .Names = c("x", 
                                                                                                                                       "y", "z"), row.names = c(NA, -10L))

##答案

library(zoo)
data$z <- rollmeanr(data$y,4,fill=NA)

答案 1 :(得分:0)

您可以将outer()与自定义功能一起使用。 diag()为您提供所需的值。

myMean <- function(x, y) mean(dat[seq(x, y), 2])
mmean <- diag(outer(1:nrow(dat), (4:nrow(dat)), Vectorize(myMean)))

dat$z <- NA  # initialize column
dat$z[-(1:3)] <- mmean

#     x  y    z
# 1   1 11   NA
# 2   2 12   NA
# 3   3 13   NA
# 4   4 14 12.5
# 5   5 15 13.5
# 6   6 16 14.5
# 7   7 17 15.5
# 8   8 18 16.5
# 9   9 19 17.5
# 10 10 20 18.5

数据

dat <- data.frame(x=rep(1:10, 1), y=rep(11:20, 1))