R 如果时间在一定间隔内,则创建指标变量

R 如果时间在一定间隔内,则创建指标变量,r,dplyr,tidyverse,lubridate,R,Dplyr,Tidyverse,Lubridate,我有一列时间是作为原始文本输入的。下面是一个示例(文章底部的数据输入代码): #>id时间 #>1 NA #> 2 1 0000-01-01 19:50:00 #> 3 2 0000-01-01 19:20:00 #> 4 3 0000-01-01 15:20:00 有没有一种简单的方法可以直接使用小时和分钟,然后创建我提到的虚拟变量 数据输入代码 df不要试图将其作为日期/时间来处理,而是使用parse_date_time的输出来计算0000-01-01午夜以来的小时数 df <

我有一列时间是作为原始文本输入的。下面是一个示例(文章底部的数据输入代码):

#>id时间
#>1 NA
#> 2  1 0000-01-01 19:50:00
#> 3  2 0000-01-01 19:20:00
#> 4  3 0000-01-01 15:20:00
有没有一种简单的方法可以直接使用小时和分钟,然后创建我提到的虚拟变量

数据输入代码
df不要试图将其作为日期/时间来处理,而是使用
parse_date_time
的输出来计算
0000-01-01
午夜以来的小时数

df <- data.frame(
  id = c(NA, 1, 2, 3),
  time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
)

library(dplyr)
library(lubridate)
df <- df %>% mutate(time = lubridate::parse_date_time(time, '%I:%M %p'), 
                    time = difftime(time, 
                                    as.POSIXct("0000-01-01", tz = "UTC"), 
                                    units = "hours"), 
                    before_1930 = as.numeric(time < 19.5),
                    between_1900_1930 = as.numeric(time > 19 & time < 19.5))
df
df 19&时间<19.5))
df

不要试图将其作为日期/时间处理,而是使用
parse\u date\u time
的输出来计算
0000-01-01
午夜后的小时数

df <- data.frame(
  id = c(NA, 1, 2, 3),
  time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
)

library(dplyr)
library(lubridate)
df <- df %>% mutate(time = lubridate::parse_date_time(time, '%I:%M %p'), 
                    time = difftime(time, 
                                    as.POSIXct("0000-01-01", tz = "UTC"), 
                                    units = "hours"), 
                    before_1930 = as.numeric(time < 19.5),
                    between_1900_1930 = as.numeric(time > 19 & time < 19.5))
df
df 19&时间<19.5))
df
试试这个:

library(dplyr)
library(lubridate)
data.frame(
   id = c(NA, 1, 2, 3),
   time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
 ) %>% 
   mutate(real_time = lubridate::parse_date_time(time, '%I:%M %p'),
          is_before = case_when(
            hour(real_time) < 19  ~ "Before 19",
            hour(real_time) == 19 & minute(real_time) < 30 ~ "19:00 - 19:30",
            T ~ "After 19:30"
          ))
  id    time           real_time     is_before
1 NA    <NA>                <NA>   After 19:30
2  1 7:50 pm 0000-01-01 19:50:00   After 19:30
3  2 7:20 pm 0000-01-01 19:20:00 19:00 - 19:30
4  3 3:20 pm 0000-01-01 15:20:00     Before 19
库(dplyr)
图书馆(lubridate)
数据帧(
id=c(NA,1,2,3),
时间=c(不适用,“下午7:50”、“下午7:20”、“下午3:20”)
) %>% 
mutate(real_time=lubridate::parse_date_time(时间,'%I:%M%p'),
是在之前还是在什么时候(
小时(实时)<19~“在19之前”,
小时(实时)==19分钟(实时)<30~“19:00-19:30”,
“19:30之后”
))
id time real\u time是以前的
19:30后1 NA
21下午7:50 0000-01-01 19:50:00 19:30之后
32下午7:20 0000-01-01 19:20:00 19:00-19:30
4 3下午3:20 0000-01-01 15:20:00 19点之前
试试这个:

library(dplyr)
library(lubridate)
data.frame(
   id = c(NA, 1, 2, 3),
   time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
 ) %>% 
   mutate(real_time = lubridate::parse_date_time(time, '%I:%M %p'),
          is_before = case_when(
            hour(real_time) < 19  ~ "Before 19",
            hour(real_time) == 19 & minute(real_time) < 30 ~ "19:00 - 19:30",
            T ~ "After 19:30"
          ))
  id    time           real_time     is_before
1 NA    <NA>                <NA>   After 19:30
2  1 7:50 pm 0000-01-01 19:50:00   After 19:30
3  2 7:20 pm 0000-01-01 19:20:00 19:00 - 19:30
4  3 3:20 pm 0000-01-01 15:20:00     Before 19
库(dplyr)
图书馆(lubridate)
数据帧(
id=c(NA,1,2,3),
时间=c(不适用,“下午7:50”、“下午7:20”、“下午3:20”)
) %>% 
mutate(real_time=lubridate::parse_date_time(时间,'%I:%M%p'),
是在之前还是在什么时候(
小时(实时)<19~“在19之前”,
小时(实时)==19分钟(实时)<30~“19:00-19:30”,
“19:30之后”
))
id time real\u time是以前的
19:30后1 NA
21下午7:50 0000-01-01 19:50:00 19:30之后
32下午7:20 0000-01-01 19:20:00 19:00-19:30
4 3下午3:20 0000-01-01 15:20:00 19点之前
df <- data.frame(
          id = c(NA, 1, 2, 3),
        time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
)
df <- data.frame(
  id = c(NA, 1, 2, 3),
  time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
)

library(dplyr)
library(lubridate)
df <- df %>% mutate(time = lubridate::parse_date_time(time, '%I:%M %p'), 
                    time = difftime(time, 
                                    as.POSIXct("0000-01-01", tz = "UTC"), 
                                    units = "hours"), 
                    before_1930 = as.numeric(time < 19.5),
                    between_1900_1930 = as.numeric(time > 19 & time < 19.5))
df
library(dplyr)
library(lubridate)
data.frame(
   id = c(NA, 1, 2, 3),
   time = c(NA, "7:50 pm", "7:20 pm", "3:20 pm")
 ) %>% 
   mutate(real_time = lubridate::parse_date_time(time, '%I:%M %p'),
          is_before = case_when(
            hour(real_time) < 19  ~ "Before 19",
            hour(real_time) == 19 & minute(real_time) < 30 ~ "19:00 - 19:30",
            T ~ "After 19:30"
          ))
  id    time           real_time     is_before
1 NA    <NA>                <NA>   After 19:30
2  1 7:50 pm 0000-01-01 19:50:00   After 19:30
3  2 7:20 pm 0000-01-01 19:20:00 19:00 - 19:30
4  3 3:20 pm 0000-01-01 15:20:00     Before 19