在R中生成cumsum函数的最小值

时间:2018-03-21 20:19:15

标签: r tidyverse cumsum

set.seed(123)
dat <- data.frame(day = 1:365, rain = runif(min = 0, max = 5,365),tmean = runif(min = 15, max = 33, 365) )
dat <- dat %>% mutate(mean.daily.rain = mean(rain),mean.daily.tmean = mean(tmean)) %>% 
mutate(rain.acc = rain - mean.daily.rain,tmean.acc = tmean - mean.daily.tmean)

如果我想查找一年中哪一天rain.acctmean.acc的cumsum值是最小值,我可以这样做:

dat %>% summarise(which.min(cumsum(rain.acc)))
329

dat %>% summarise(which.min(cumsum(tmean.acc)))
159

但是,我想强加一个条件,我只想看看doy&gt; = 213和&lt; = 365,即如何提取213到365之间的日期,{{1}的最低值}和cumsum(rain.acc)。请注意,cumsum(tmean.acc)必须在整年内计算。

4 个答案:

答案 0 :(得分:3)

使用ifelse()

对可能的值应用过滤器
fun = function(x, i, min, max)
    which.min(cumsum(x) * ifelse(i >= min & i <= max, 1, NA))

> fun(dat$tmean.acc, dat$day, 213, 365)
[1] 248

> dat %>% summarize(
    rain.min = fun(rain.acc, day, 213, 365),
    tmean.min = fun(tmean.acc, day, 213, 365)
  )
  rain.min tmean.min
1      329       248

> filter(dat, row_number() == fun(tmean.acc, day, 213, 365))
  day     rain    tmean mean.daily.rain mean.daily.tmean rain.acc tmean.acc
1 248 4.846782 15.39589          2.4938         24.03155 2.352982 -8.635665

答案 1 :(得分:2)

注意:您必须添加212才能获得一年中正确的日期。

使用基础R

with(dat, which.min(cumsum(rain - mean(rain))[day>=213 & day<=365]) ) + 212  # 329
with(dat, which.min(cumsum(tmean - mean(tmean))[day>=213 & day<=365]) ) + 212  # 248

使用data.table包

library('data.table')
setDT(dat)

# calculate cumsum over the entire year
dat[ , rain.acc := cumsum(rain - mean(rain)) ]
dat[ , tmean.acc := cumsum(tmean - mean(tmean)) ]

# For entire data    
dat[ dat[ , which.min( rain.acc) ], ]
#    day     rain    tmean  rain.acc tmean.acc
# 1: 329 1.691956 17.52186 -5.548483  13.31113
dat[ dat[ , which.min( tmean.acc) ], ]
#    day    rain    tmean  rain.acc tmean.acc
# 1: 159 2.22384 15.67266 0.1829257 -79.17573

# For data within a specified range    
dat[ dat[ day >=213 & day <= 365, which.min( rain.acc) + 213 - 1 ], ]
#    day     rain    tmean  rain.acc tmean.acc
# 1: 329 1.691956 17.52186 -5.548483  13.31113
dat[ dat[ day >=213 & day <= 365, which.min( tmean.acc) + 213 - 1 ], ]
#    day     rain    tmean rain.acc tmean.acc
# 1: 248 4.846782 15.39589 7.623054  -37.2419

答案 2 :(得分:1)

你可以在服用cumsum之后但在之前使用.min?

dat %>% summarise(which.min(cumsum(rain.acc)[day>=213&day<=365]))

答案 3 :(得分:1)

一种选择是将filter用于第一个子集行,然后将条件与row_number()匹配,以找到确切的行:

  library(dplyr)

  dat %>%
    filter(day >= 213 & day <= 365) %>%
    filter(row_number() == which.min(cumsum(rain.acc)))
  # day     rain    tmean mean.daily.rain mean.daily.tmean   rain.acc tmean.acc
  # 1 329 1.691956 17.52186          2.4938         24.03155 -0.8018434 -6.509688

  dat %>%
    filter(day >= 213 & day <= 365) %>%
    filter(row_number() == which.min(cumsum(tmean.acc)))
  # day     rain    tmean mean.daily.rain mean.daily.tmean rain.acc tmean.acc
  # 1 248 4.846782 15.39589          2.4938         24.03155 2.352982 -8.635665