【问题标题】:Correction of hourly precipitation data with conditions带条件的每小时降水数据修正
【发布时间】:2020-11-15 11:21:06
【问题描述】:

我正在处理降水数据,并希望实施一个校正方案。为此,我有经验常数,我必须在 12 小时间隔内将其应用于测量。

  • 如果在 12 小时范围内至少有一个降水测量值 != 0 & != NA,则将常数除以该测量值,然后将商添加到测量值中
  • 如果区间内没有值 > 0 & != NA,则什么也不做(或加 0)

我真的不知道如何或从哪里开始。

这是一些随机降水数据,经验常数为 0.14 [mm/12h]:

set.seed(1)
Time <- seq(from = as.POSIXct("2012-05-15 07:00"), 
                  to = as.POSIXct("2012-06-15 07:00"), by = "hour")
Precipitation <- runif(Time, min=0, max = 20)
Precipitation[Precipitation >5] <- 0
Precipitation[Precipitation >4.5] <- NA
  
df <- data.frame(Time, Precipitation)

编辑:输出应该看起来像同一个表,其中添加了仅包含商的列。 Edit2:这是两个 12 小时间隔的示例,它们显示了我想要实现的目标。抱歉,有点不清楚。

【问题讨论】:

  • 您还可以添加预期输出的样子吗?
  • 我现在举个例子

标签: r


【解决方案1】:

我不能 100% 确定我对您的问题的理解正确,但请考虑以下代码。

编辑:编辑问题后,我稍微修改了代码。现在只有左商的列。以下是对您有用的代码。


代码

Rows = as.numeric(rownames(df[seq(1, nrow(df), 12), ]))
Twelvehour_list = split(df, cumsum(1:nrow(df) %in% (Rows)))
Solution_list = lapply(Twelvehour_list, function(x)   
           {if(length(x$Precipitation[x$Precipitation > 0]) == 0) {
                                     x$newcol <- empiric_constant; x
                                } else {
                                  x$newcol <- 
                                      empiric_constant/length(x$Precipitation[x$Precipitation > 0 !is.na(x$Precipitation)]); 
                        x$newcol[x$Precipitation == 0] <- 0;
                        x$newcol[is.na(x$Precipitation)] <- 0;
                        x
                                }

                                    })

输出

 head(Solution_list[[10]])
               Time Precipitation newcol   
 118 2012-05-20 04:00:00      2.063685  0.125 
 119 2012-05-20 05:00:00      0.000000  0.000 
 120 2012-05-20 06:00:00      0.000000  0.000 
 121 2012-05-20 07:00:00      0.000000  0.000 
 122 2012-05-20 08:00:00      0.000000  0.000 
 123 2012-05-20 09:00:00      0.000000  0.000 

输出是data.frames 的列表,每个跨越 12 小时,包含商列和修改后的降水数据列,即添加了经验常数。这就是你的想法吗?

【讨论】:

  • 我用一个例子指定了我的请求。基本上我想将常数(0.14)除以 12 小时间隔内的观察量 !=0 和 !=NA ,并将结果显示在这些观察行的新列中。
  • 好的,我明白了,谢谢!有没有办法将列表合并到一个与原始数据相似的 data.frame 中?
  • 如果区间不包含任何测量 > 0 或 != NA,则根据您的解决方案将整个常数添加到每个测量中。我尝试修复它,但我对这些代码真的很陌生......
  • 这个对我有用:Rows = as.numeric(rownames(df[seq(1, nrow(df), 12), ])) Twelvehour_list = split(df, cumsum(1:nrow(df) %in% (Rows))) Solution_list = lapply(Twelvehour_list, function(x) {if(length(x$Precipitation[x$Precipitation &gt; 0]) == 0) { x$newcol &lt;- 0; x} else { x$newcol &lt;- empiric_constant/length(x$Precipitation[x$Precipitation &gt; 0 &amp; !is.na(x$Precipitation)]); x$newcol[x$Precipitation == 0] &lt;- 0; x$newcol[is.na(x$Precipitation)] &lt;- 0;x}})
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 2016-07-28
相关资源
最近更新 更多