gpt4 book ai didi

r - 计算向量中每 n 个值的平均值

转载 作者:行者123 更新时间:2023-12-04 11:00:47 24 4
gpt4 key购买 nike

所以让我们说我有一个向量

a <- rnorm(6000)

我想计算第 1 个值到第 60 个值的平均值,然后再次计算第 61 个值到第 120 个值的平均值,以此类推。所以基本上我想计算每 60 个值的平均值,从该向量给我 100 个平均值。我知道我可以做一个 for 循环,但我想知道是否有更好的方法来做到这一点?

最佳答案

我会用

 colMeans(matrix(a, 60))
.colMeans(a, 60, length(a) / 60) # more efficient (without reshaping to matrix)

用户增强 adunaic的请求

This only works if there are 60x100 data points. If you have an incomplete 60 at the end then this errors. It would be good to have a general solution for others looking at this problem for ideas.


BinMean <- function (vec, every, na.rm = FALSE) {
n <- length(vec)
x <- .colMeans(vec, every, n %/% every, na.rm)
r <- n %% every
if (r) x <- c(x, mean.default(vec[(n - r + 1):n], na.rm = na.rm))
x
}

a <- 1:103
BinMean(a, every = 10)
# [1] 5.5 15.5 25.5 35.5 45.5 55.5 65.5 75.5 85.5 95.5 102.0

分组操作的替代解决方案(效率较低)
BinMean2 <- function (vec, every, na.rm = FALSE) {
grp <- as.integer(ceiling(seq_along(vec) / every))
grp <- structure(grp, class = "factor",
levels = as.character(seq_len(grp[length(grp)])) )
lst <- .Internal(split(vec, grp))
unlist(lapply(lst, mean.default, na.rm = na.rm), use.names = FALSE)
}

速度
library(microbenchmark)
a <- runif(1e+4)
microbenchmark(BinMean(a, 100), BinMean2(a, 100))
#Unit: microseconds
# expr min lq mean median uq max
# BinMean(a, 100) 40.400 42.1095 54.21286 48.3915 57.6555 205.702
# BinMean2(a, 100) 1216.823 1335.7920 1758.90267 1434.9090 1563.1535 21467.542

关于r - 计算向量中每 n 个值的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43635846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com