【问题标题】:Using the mutate function & making conditional statements, r使用 mutate 函数并制作条件语句,r
【发布时间】:2020-08-03 15:04:18
【问题描述】:

我想将日期和时间段的间隔合并到条件语句中。问题出在“天”确定的某个地方。我不认为包含上午和下午时间是全天候的——我可以使用什么语法来代替?

## 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"
                        )
                      ) 

【问题讨论】:

  • 请用dput 展示一个可重复的小示例,因为不清楚您的数据集列的类别
  • 我用 # 创建新的拆分日期和时间列 dml$ObservationDate
  • krovinski,我相信 akrun 是在要求您提供一些我们可以使用的东西。因为我们对dml 一无所知,所以即使知道你在做format 也无助于我们从一个起点开始。而且由于 cmets 很容易隐藏或跳过,因此通常最好通过 editing 您的问题并将其放置在那里(在代码块内)来保持问题“完整”。谢谢。
  • r2evans & akrun - 感谢您的跟进 - 我意识到我需要一个“或”语法

标签: r syntax


【解决方案1】:
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"
                        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2015-09-12
    • 2018-08-05
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多