gpt4 book ai didi

r 沿向量搜索并计算平均值

转载 作者:行者123 更新时间:2023-12-04 21:52:01 24 4
gpt4 key购买 nike

我有如下数据:

require(data.table)
DT <- data.table(x=c(19,19,19,21,21,19,19,22,22,22),
y=c(53,54,55,32,44,45,49,56,57,58))

我想沿着 x 搜索,并计算 y 的平均值。
但是,在使用的时候。
DT[, .(my=mean(y)), by=.(x)]

我得到了 x 重合值的总体均值。
我想沿着 x 搜索,每次 x 变化时,我想计算一个新的平均值。对于提供的示例,输出将是:
DTans <- data.table(x=c(19,21,19,22),
my=c(54,38,47,57))

最佳答案

我们可以使用 rleid要创建另一个分组变量,请获取 mean 'y',并将 'indx' 分配给 NULL

library(data.table) # v 1.9.5+
DT[, .(my = mean(y)), by = .(indx = rleid(x), x)][, indx := NULL]
# x my
#1: 19 54
#2: 21 38
#3: 19 47
#4: 22 57

基准
set.seed(24)
foo <- function(x) sample(x, 1e7L, replace = TRUE)
DT <- data.table(x = foo(100L), y = foo(10000L))

josilber <- function() {
new.group <- c(1, diff(DT$x) != 0)
res <- data.table(x = DT$x[new.group == 1],
my = tapply(DT$y, cumsum(new.group), mean))
}

Roland <- function() {
DT[, .(my = mean(y), x = x[1]), by = cumsum(c(1, diff(x) != 0))]
}

akrun <- function() {
DT[, .(my = mean(y)), by = .(indx = rleid(x), x)][,indx := NULL]
}

bgoldst <- function() {
with(rle(DT$x), data.frame(x = values,
my = tapply(DT$y, rep(1:length(lengths), lengths), mean)))
}

system.time(josilber())
# user system elapsed
#159.405 1.759 161.110

system.time(bgoldst())
# user system elapsed
#162.628 0.782 163.380

system.time(Roland())
# user system elapsed
# 18.633 0.052 18.678

system.time(akrun())
# user system elapsed
# 1.242 0.003 1.246

关于r 沿向量搜索并计算平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685733/

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