如果条件仅适用于两小时,则设定一整天的价值

时间:2018-01-20 13:29:20

标签: r if-statement

编辑:我找到了一个不使用其他软件包的解决方案。还是,感谢那些给出答案的人。

for (Z in 1:nrow(MyData)) {Today= substr(MyData[Z,1], 1, 10)
  if(any(MyData$Rain[((MyData$Time>= strptime(paste(Today, "06:30:00", sep = " "),format="%Y-%m-%d %H:%M:%S"))&
                 (MyData$Time<= strptime(paste(Today, "09:00:00", sep = " "),format="%Y-%m-%d %H:%M:%S")))]>0)) {MyData$Rain_Binary[(MyData$Time>=strptime(paste(Today, "06:30:00", sep = " "),format="%Y-%m-%d %H:%M:%S"))&
                                                                                                                                       (MyData$Time<= strptime(paste(Today, "20:00:00", sep = " "),format="%Y-%m-%d %H:%M:%S"))]=1
  }
}

我有一个这样的数据框:

start <- as.POSIXct("2015-05-05 06:00:00", format="%Y-%m-%d %H:%M:%S")
end   <- as.POSIXct("2015-05-07 20:00:00", format= "%Y-%m-%d %H:%M:%S")
Time=seq.POSIXt(start, end, by="10 min")

MyData=data.frame(Time=Time,Rain=rep(0,(length(Time))),Rain_Binary=rep(0,(length(Time))))
MyData[c(2:4,154:157,324:328),2]=5

如果在此期间下雨,我想将Rain_Binary列中的值设置为1:

Limit=c(substr(MyData$Time[MyData$Time>= strptime("2015-05-05 06:30:00", "%Y-%m-%d %H:%M:%S") & MyData$Time <= strptime("2015-05-05 09:00:00", "%Y-%m-%d %H:%M:%S")],12,19))

MyData$Rain_Binary=with(MyData, ifelse(((MyData$Rain>0) &  (substr(MyData$Time,12,19) %in% Limit)), 1, 0))

这是按预期工作的。 我现在的问题是,如果在限制中指定的时间内下雨,并且不仅在指定的时间内下雨,我想将整个日期的值设置为1。 我该怎么做?

2 个答案:

答案 0 :(得分:1)

group_by日期并使用any

library('tidyverse')

MyData %>%
  group_by(as.Date(Time)) %>%
  mutate(Rain_Binary = any(Rain_Binary))

答案 1 :(得分:0)

以下是使用data.tablelubridate的方法。假设您的data.frame只有两列(RainTime),并且您有一个接收Timerain的函数,并根据if输出二进制数符号是否满足,称为func_rain

library(data.table)
start <- as.POSIXct("2015-05-05 06:00:00", format="%Y-%m-%d %H:%M:%S")
end   <- as.POSIXct("2015-05-07 20:00:00", format= "%Y-%m-%d %H:%M:%S")
dt=data.table(Time=seq.POSIXt(start, end,length.out = 300),
              Rain=round(runif(300)*2))

rain_func <- function(Rain, Time){
  st <- as.POSIXct("2015-05-05 06:30:00", format = "%Y-%m-%d %H:%M:%S")
  et <- as.POSIXct("2015-05-05 9:00:00", format = "%Y-%m-%d %H:%M:%S")
  ifelse(Time >= st & Time <= et, 1, 0)
}

dt[, newval := rain_func(Rain, Time)][, dayval := any(newval), by = lubridate::round_date(Time, "1 day")]

<强>输出

> dt
                    Time Rain newval dayval
  1: 2015-05-05 06:00:00    1      0   TRUE
  2: 2015-05-05 06:12:26    0      0   TRUE
  3: 2015-05-05 06:24:52    2      0   TRUE
  4: 2015-05-05 06:37:19    2      1   TRUE
  5: 2015-05-05 06:49:45    0      1   TRUE
 ---                                       
296: 2015-05-07 19:10:14    2      0  FALSE
297: 2015-05-07 19:22:40    0      0  FALSE
298: 2015-05-07 19:35:07    0      0  FALSE
299: 2015-05-07 19:47:33    0      0  FALSE
300: 2015-05-07 20:00:00    2      0  FALSE

该函数用于根据您的逻辑为给定的时间间隔创建二进制值,然后使用链接设置当天的值。

使用函数代替硬编码的if else语句可以更轻松地将这些语句应用于多个列/数据集,round_date在查看奇数间隔时非常有用,例如round_date(Time, "37 min")