R:分别计算每个因子水平,然后计算水平上的最小/平均/最大

时间:2018-09-13 07:41:51

标签: r factors levels

所以我确实有一个水分配模型的输出,该模型是河流每小时的流入和流出值。我已经完成了5次模型运行

可复制的示例:

df <- data.frame(rep(seq(
                  from=as.POSIXct("2012-1-1 0:00", tz="UTC"),
                  to=as.POSIXct("2012-1-1 23:00", tz="UTC"),
                  by="hour"
                  ),5),
                as.factor(c(rep(1,24),rep(2,24),rep(3,24), rep(4,24),rep(5,24))),
                rep(seq(1,300,length.out=24),5),
                rep(seq(1,180, length.out=24),5) )

colnames(df)<-c("time", "run", "inflow", "discharge")

当然,实际上,运行的值是变化的。 (而且我确实有很多数据,因为我确实有100次运行,每小时值是35年。)

因此,起初,我想计算每次运行的缺水率,这意味着我需要计算类似(1-(排放量/流入时间为6小时)),因为水需要运行6小时通过集水区。

 scarcityfactor <- 1 - (discharge / lag(inflow,6))

然后,我想计算所有运行的稀缺性因子的平均值,最大值和最小值(以找出每个时间步长可能出现的最高,最低和平均值的稀疏性;根据不同的模型运行)。所以我想说,我可以计算每个时间步长的平均值,最大值和最小值:

f1 <- function(x) c(Mean = (mean(x)), Max = (max(x)), Min = (min(x)))
results <- do.call(data.frame, aggregate(scarcityfactor ~ time, 
      data = df,                                                              
      FUN = f1))

有人可以帮我提供代码吗?

2 个答案:

答案 0 :(得分:1)

如果我正确理解问题描述,我相信这就是您想要的。

我将使用data.table

library(data.table)
setDT(df)

# add scarcity_factor (group by run)
df[ , scarcity_factor := 1 - discharge/shift(inflow, 6L), by = run]

# group by time, excluding times for which the
#   scarcity factor is missing
df[!is.na(scarcity_factor), by = time,
   .(min_scarcity = min(scarcity_factor),
     mean_scarcity = mean(scarcity_factor),
     max_scarcity = max(scarcity_factor))]

#                    time  min_scarcity mean_scarcity  max_scarcity
#  1: 2012-01-01 06:00:00 -46.695652174 -46.695652174 -46.695652174
#  2: 2012-01-01 07:00:00  -2.962732919  -2.962732919  -2.962732919
#  3: 2012-01-01 08:00:00  -1.342995169  -1.342995169  -1.342995169
#  4: 2012-01-01 09:00:00  -0.776086957  -0.776086957  -0.776086957
#  5: 2012-01-01 10:00:00  -0.487284660  -0.487284660  -0.487284660
#  6: 2012-01-01 11:00:00  -0.312252964  -0.312252964  -0.312252964
#  7: 2012-01-01 12:00:00  -0.194826637  -0.194826637  -0.194826637
#  8: 2012-01-01 13:00:00  -0.110586011  -0.110586011  -0.110586011
#  9: 2012-01-01 14:00:00  -0.047204969  -0.047204969  -0.047204969
# 10: 2012-01-01 15:00:00   0.002210759   0.002210759   0.002210759
# 11: 2012-01-01 16:00:00   0.041818785   0.041818785   0.041818785
# 12: 2012-01-01 17:00:00   0.074275362   0.074275362   0.074275362
# 13: 2012-01-01 18:00:00   0.101356965   0.101356965   0.101356965
# 14: 2012-01-01 19:00:00   0.124296675   0.124296675   0.124296675
# 15: 2012-01-01 20:00:00   0.143977192   0.143977192   0.143977192
# 16: 2012-01-01 21:00:00   0.161047028   0.161047028   0.161047028
# 17: 2012-01-01 22:00:00   0.175993343   0.175993343   0.175993343
# 18: 2012-01-01 23:00:00   0.189189189   0.189189189   0.189189189

通过lapply遍历不同的聚合器,您可以变得更加简洁:

df[!is.na(scarcity_factor), by = time,
   lapply(list(min, mean, max), function(f) f(scarcity_factor))]

最后,您可以将其视为通过聚合进行重塑并使用dcast

dcast(df, time ~ ., value.var = 'scarcity_factor',
      fun.aggregate = list(min, mean, max))

(如果要排除无意义的行,请在df[!is.na(scarcity_factor)]的第一个参数中使用dcast

答案 1 :(得分:0)

library(tidyverse)

df %>%
  group_by(run) %>%
  mutate(scarcityfactor = 1 - discharge / lag(inflow,6)) %>%
  group_by(time) %>%
  summarise(Mean = mean(scarcityfactor), 
            Max = max(scarcityfactor), 
            Min = min(scarcityfactor))

# # A tibble: 24 x 4
#  time                   Mean     Max     Min
#  <dttm>                <dbl>   <dbl>   <dbl>
# 1 2012-01-01 00:00:00  NA      NA      NA    
# 2 2012-01-01 01:00:00  NA      NA      NA    
# 3 2012-01-01 02:00:00  NA      NA      NA    
# 4 2012-01-01 03:00:00  NA      NA      NA    
# 5 2012-01-01 04:00:00  NA      NA      NA    
# 6 2012-01-01 05:00:00  NA      NA      NA    
# 7 2012-01-01 06:00:00 -46.7   -46.7   -46.7  
# 8 2012-01-01 07:00:00  -2.96   -2.96   -2.96 
# 9 2012-01-01 08:00:00  -1.34   -1.34   -1.34 
#10 2012-01-01 09:00:00  -0.776  -0.776  -0.776
# # ... with 14 more rows