首先生成一些样本数据:
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$doy的累加和为>= thres时,计算df$value
下面是我用来实现这一点的长for loop:
# 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
然而,我真的想避免这种循环。有没有什么方法可以用更少的代码来完成这件事?
https://stackoverflow.com/questions/47772458
复制相似问题