gpt4 book ai didi

r - 避免对 cumsum 使用 for 循环

转载 作者:行者123 更新时间:2023-12-04 06:02:54 24 4
gpt4 key购买 nike

首先生成一些示例数据:

 doy <- rep(1:365,times=2)
year <- rep(2000:2001,each=365)
set.seed(1)
value <-runif(min=0,max=10,365*2)
doy.range <- c(40,50,60,80)
thres <- 200

df <- data.frame(cbind(doy,year,value))

我想做的是:

对于 df$year == 2000,从 doy.range == 40 开始,开始添加df$value,当df$value的累计和>=thres时,计算df$doy/p>

这是我用来实现此目的的长 for 循环:

# create a matrix to store results

mat <- matrix(, nrow = length(doy.range)*length(unique(year)),ncol=3)
mat[,1] <- rep(unique(year),each=4)
mat[,2] <- rep(doy.range,times=2)

for(i in unique(df$year)){

dat <- df[df$year== i,]

for(j in doy.range){

dat1 <- dat[dat$doy >= j,]
dat1$cum.sum <-cumsum(dat1$value)
day.thres <- dat1[dat1$cum.sum >= thres,"doy"][1] # gives me the doy of the year where cumsum of df$value becomes >= thres
mat[mat[,2] == j & mat[,1] == i,3] <- day.thres
}
}

cumsum$value 超过 thres 时,此循环会在矩阵的第三列中给出 doy

但是,我真的很想避免循环。有什么方法可以使用更少的代码来做到这一点吗?

最佳答案

如果我理解正确,您可以使用 dplyr。假设阈值为 200:

library(dplyr)
df %>% group_by(year) %>%
filter(doy >= 40) %>%
mutate(CumSum = cumsum(value)) %>%
filter(CumSum >= 200) %>%
top_n(n = -1, wt = CumSum)

产生

# A tibble: 2 x 4
# Groups: year [2]
doy year value CumSum
<dbl> <dbl> <dbl> <dbl>
1 78 2000 3.899895 201.4864
2 75 2001 9.205178 204.3171

我猜所使用的动词是不言自明的。如果没有,请告诉我。

对于不同的doy创建一个函数并使用lapply:

f <- function(doy.range) {
df %>% group_by(year) %>%
filter(doy >= doy.range) %>%
mutate(CumSum = cumsum(value)) %>%
filter(CumSum >= 200) %>%
top_n(n = -1, wt = CumSum)
}

lapply(doy.range, f)

[[1]]
# A tibble: 2 x 4
# Groups: year [2]
doy year value CumSum
<dbl> <dbl> <dbl> <dbl>
1 78 2000 3.899895 201.4864
2 75 2001 9.205178 204.3171

[[2]]
# A tibble: 2 x 4
# Groups: year [2]
doy year value CumSum
<dbl> <dbl> <dbl> <dbl>
1 89 2000 2.454885 200.2998
2 91 2001 6.578281 200.6544

[[3]]
# A tibble: 2 x 4
# Groups: year [2]
doy year value CumSum
<dbl> <dbl> <dbl> <dbl>
1 98 2000 4.100841 200.5048
2 102 2001 7.158333 200.3770

[[4]]
# A tibble: 2 x 4
# Groups: year [2]
doy year value CumSum
<dbl> <dbl> <dbl> <dbl>
1 120 2000 6.401010 204.9951
2 120 2001 5.884192 200.8252

关于r - 避免对 cumsum 使用 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772458/

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