简体   繁体   English

使用变异 function 并制作条件语句,r

[英]Using the mutate function & making conditional statements, r

I have intervals of dates and time periods that I want to incorporate into a conditional statement.我想将日期和时间段的间隔合并到条件语句中。 The problem lies somewhere in the "day" determination.问题出在“天”确定的某个地方。 I don't think it's going around the clock to include the am and pm times- what syntax could I use instead?我不认为包含上午和下午时间是全天候的——我可以使用什么语法来代替?

## 6.2 Narrative (Intervals) 
# Interval 1
  ## Interval Date Start  "2019-09-23"
  ## Interval Date End    "2019-10-05"
  ## Day Start Time       "21:01:00"
  ## Day End Time         "12:01:00"
  ## Night Start Time     "12:00:00"
  ## Night End Time       "21:00:00"
  ## Other Time
# Interval 2
  ## Interval Date Start  "2019-10-05"
  ## Interval Date End    "2019-10-30"
  ## Day Start Time       "21:01:00"
  ## Day End Time         "12:29:00"
  ## Night Start Time     "12:30:00"
  ## Night End Time       "21:00:00"
  ## Other Time

##6.3 Applying the tidyverse 
# Using the "case_when" function in the tidyverse in the place of a loop

    dml <- dml %>% mutate(period=case_when(
                         (ObservationDate >= "2019-09-23") 
                          & (ObservationDate <="2019-10-05") 
                          & (ObservationTime >= "12:00:00") 
                          & (ObservationTime <="21:00:00") ~"night",

                          (ObservationDate >= "2019-10-05")
                          & (ObservationDate <= "2019-10-30")
                          & (ObservationTime >= "12:30:00") 
                          & (ObservationTime <="21:00:00") ~"night",

                          (ObservationDate >= "2019-09-23") 
                          & (ObservationDate <="2019-10-05")
                          & (ObservationTime >= "21:01:00") 
                          & (ObservationTime <="11:59:00") ~"day",

                          (ObservationDate >= "2019-10-05")
                          & (ObservationDate <= "2019-10-30")
                          & (ObservationTime >= "21:01:00")
                          & (ObservationTime <= "12:29:00") ~"day",
                          TRUE ~"other"
                        )
                      ) 
r2evans & akrun - thanks for following up - I realized I need an "or" syntax to complete the 24hours in a day- thanks           
     dml <- dml %>% mutate(period=case_when(
                          (ObservationDate >= "2019-09-23") 
                          & (ObservationDate <="2019-10-05") 
                          & (ObservationTime >= "12:00:00") 
                          & (ObservationTime <="21:00:00") ~"night",

                          (ObservationDate >= "2019-10-05")
                          & (ObservationDate <= "2019-10-30")
                          & (ObservationTime >= "12:30:00") 
                          & (ObservationTime <="21:00:00") ~"night",

                          (ObservationDate >= "2019-09-23") 
                          & (ObservationDate <="2019-10-05")
                          & ((ObservationTime >= "21:01:00") 
                          | (ObservationTime <="11:59:00")) ~"day",

                          (ObservationDate >= "2019-10-05")
                          & (ObservationDate <= "2019-10-30")
                          & ((ObservationTime >= "21:01:00")
                          | (ObservationTime <= "12:29:00")) ~"day",
                          TRUE ~"other"
                        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM