【问题标题】:How to determine if a time is between two other times in R?如何确定时间是否在R中的其他两个时间之间?
【发布时间】:2020-11-03 02:53:37
【问题描述】:

这是我的数据框:

  TIME      data   sunrise     sunset     
  <chr>     <chr>  <chr>       <chr>      
1 6:00 AM   ddd    07:19:53 AM 04:41:13 PM
2 1:15 PM   fff    07:19:53 AM 04:41:13 PM
3 01:00 AM  rrrr   07:19:52 AM 04:42:08 PM
4 10:28 AM  btv    07:19:52 AM 04:42:08 PM
5 11:45 AM  gored  07:19:52 AM 04:42:08 PM

我想创建一个新列来指示TIME 是否介于sunrisesunset 之间。

我认为它会像 ifelse 一样简单,但这不起作用,我认为这是因为字符...

df  %>% 
  mutate(daylight = ifelse(between(TIME, sunrise,sunset) == TRUE,1,0))

应该是这样的:

  TIME      data   sunrise     sunset       daylight
  <chr>     <chr>  <chr>       <chr>      
1 6:00 AM   ddd    07:19:53 AM 04:41:13 PM  0
2 1:15 PM   fff    07:19:53 AM 04:41:13 PM  1
3 01:00 AM  rrrr   07:19:52 AM 04:42:08 PM  0
4 10:28 AM  btv    07:19:52 AM 04:42:08 PM  1
5 11:45 AM  gored  07:19:52 AM 04:42:08 PM  1

数据

df <- structure(list(TIME = c("6:00 AM", "1:15 PM", "1:00 AM", "10:28 AM", 
"11:45 AM"), sunrise = c("07:19:53 AM", "07:19:53 AM", "07:19:52 AM", 
"07:19:52 AM", "07:19:52 AM"), sunset = c("04:41:13 PM", "04:41:13 PM", 
"04:42:08 PM", "04:42:08 PM", "04:42:08 PM")), row.names = c(NA, 
-5L), groups = structure(list(.rows = structure(list(1L, 2L, 
    3L, 4L, 5L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), class = c("rowwise_df", "tbl_df", "tbl", 
"data.frame"))

【问题讨论】:

  • between 没有针对开始和结束进行矢量化。最好使用&lt;&gt;df %&gt;% mutate(daylight = as.integer(TIME &gt;= sunrise &amp; TIME &lt;= sunset))
  • 这在这里不起作用......
  • 这是因为它不是日期时间对象。我会首先将所有内容都转换为日期时间
  • df %&gt;% ungroup %&gt;% mutate(across(everything(), ~parse_date_time(., c('HM', 'HMS')))) %&gt;% transmute(daylight = as.integer(TIME &gt;= sunrise &amp; TIME &lt;= sunset))怎么样
  • 在完整的数据框中还有其他几个变量;只能对这三个变量进行操作

标签: r time dplyr lubridate


【解决方案1】:

我们使用POSIXct 转换为日期时间类,将其更改为ITime 并进行比较

library(dplyr)
library(data.table)
df %>%
   mutate(TIME = as.ITime(as.POSIXct(TIME, format = '%I:%M %p')), 
          sunrise = as.ITime(as.POSIXct(sunrise, format = '%I:%M:%S %p')), 
          sunset = as.POSIXct(sunset, format = '%I:%M:%S %p'), 
          daylight = as.integer(TIME >= sunrise & TIME <= sunset))
#      TIME  data  sunrise              sunset daylight
#1 06:00:00   ddd 07:19:53 2020-07-13 16:41:13        0
#2 13:15:00   fff 07:19:53 2020-07-13 16:41:13        1
#3 01:00:00  rrrr 07:19:52 2020-07-13 16:42:08        0
#4 10:28:00   btv 07:19:52 2020-07-13 16:42:08        1
#5 11:45:00 gored 07:19:52 2020-07-13 16:42:08        1

数据

df <- structure(list(TIME = c("6:00 AM", "1:15 PM", "01:00 AM", "10:28 AM", 
"11:45 AM"), data = c("ddd", "fff", "rrrr", "btv", "gored"), 
    sunrise = c("07:19:53 AM", "07:19:53 AM", "07:19:52 AM", 
    "07:19:52 AM", "07:19:52 AM"), sunset = c("04:41:13 PM", 
    "04:41:13 PM", "04:42:08 PM", "04:42:08 PM", "04:42:08 PM"
    )), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2012-11-17
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多