[英]Assign Value to Column Based on Range of Two Columns in R
I am trying to assign a TRUE or FALSE to a new column (Working Hours) based on two columns time.我正在尝试根据两列时间为新列(工作时间)分配 TRUE 或 FALSE。 I would like to state TRUE when time is within a certain range and FALSE when time is outside of that range.
当时间在某个范围内时,我想声明为 TRUE,当时间超出该范围时,我想声明为 FALSE。 I am using the following code - but having issues with it properly assigning the TRUE/FALSE values:
我正在使用以下代码 - 但在正确分配 TRUE/FALSE 值时遇到问题:
My df is called ALLOBS and column From contains the time started and column To contains the time completed.我的 df 被称为 ALLOBS,列 From 包含开始的时间,列 To 包含完成的时间。
Date From To Location Working Hours
1 1/28/2010 00:00 07:45 No Location NA
2 3/2/2010 07:45 16:30 Kahana NA
3 3/3/2010 08:00 16:15 Waikiki NA
4 3/4/2010 09:00 16:45 Kahana NA
5 3/5/2010 16:31 00:00 No Location NA
6 3/6/2010 12:00 13:15 Waikiki NA
7 3/7/2010 13:14 16:31 Kahana NA
8 3/8/2010 07:00 12:00 Honolulu NA
I would like to write TRUE when the time is between 07:30 (From) and 16:30 (To), and FALSE when the time is between 16:31 (To) and 07:29 (From).当时间在 07:30 (From) 和 16:30 (To) 之间时,我想写 TRUE,当时间在 16:31 (To) 和 07:29 (From) 之间时写 FALSE。 Thank you!
谢谢!
Making some assumptions about what your data look like and which of two options below you desire.对您的数据是什么样子以及您希望下面的两个选项中的哪一个做出一些假设。 Uses
dplyr
and lubridate
使用
dplyr
和lubridate
The question is what to do about row 5 where the From
is definitely after 07:30 and the To
is technically before 16:30 (although it is probably meant to be 00:00 the next day or 23:59 the same day?问题是如何处理第 5 行,其中
From
肯定在 07:30 之后,而To
在技术上在 16:30 之前(尽管它可能意味着第二天的 00:00 或同一天的 23:59?
ALLOBS
#> Date From To Location
#> 1 1/28/2010 00:00 07:45 No Location
#> 2 3/2/2010 07:45 16:30 Kahana
#> 3 3/3/2010 08:00 16:15 Waikiki
#> 4 3/4/2010 09:00 16:45 Kahana
#> 5 3/5/2010 16:31 00:00 No Location
#> 6 3/6/2010 12:00 13:15 Waikiki
#> 7 3/7/2010 13:14 16:31 Kahana
#> 8 3/8/2010 07:00 12:00 Honolulu
Option #1 is what I think you want making row # 5 FALSE
by checking for From
< 16:31选项 #1 是我认为您想要通过检查
From
< 16:31 使第 5 行FALSE
library(dplyr)
library(lubridate)
ALLOBS %>%
mutate(Working_hours =
hms(paste0(From, ":00")) >= hms("07:30:00") &
hms(paste0(To, ":00")) < hms("16:31:00") &
hms(paste0(From, ":00")) < hms("16:30:00")
)
#> Date From To Location Working_hours
#> 1 1/28/2010 00:00 07:45 No Location FALSE
#> 2 3/2/2010 07:45 16:30 Kahana TRUE
#> 3 3/3/2010 08:00 16:15 Waikiki TRUE
#> 4 3/4/2010 09:00 16:45 Kahana FALSE
#> 5 3/5/2010 16:31 00:00 No Location FALSE
#> 6 3/6/2010 12:00 13:15 Waikiki TRUE
#> 7 3/7/2010 13:14 16:31 Kahana FALSE
#> 8 3/8/2010 07:00 12:00 Honolulu FALSE
Option #2 omits that check and therefore concludes TRUE
选项 #2 省略了检查,因此得出
TRUE
ALLOBS %>%
mutate(Working_hours = hms(paste0(From, ":00")) > hms("07:30:00") &
hms(paste0(To, ":00")) < hms("16:31:00"))
#> Date From To Location Working_hours
#> 1 1/28/2010 00:00 07:45 No Location FALSE
#> 2 3/2/2010 07:45 16:30 Kahana TRUE
#> 3 3/3/2010 08:00 16:15 Waikiki TRUE
#> 4 3/4/2010 09:00 16:45 Kahana FALSE
#> 5 3/5/2010 16:31 00:00 No Location TRUE
#> 6 3/6/2010 12:00 13:15 Waikiki TRUE
#> 7 3/7/2010 13:14 16:31 Kahana FALSE
#> 8 3/8/2010 07:00 12:00 Honolulu FALSE
Your data (I think)你的数据(我认为)
ALLOBS <- data.frame(
Date = c("1/28/2010", "3/2/2010", "3/3/2010",
"3/4/2010", "3/5/2010", "3/6/2010",
"3/7/2010", "3/8/2010"),
From = c("00:00", "07:45", "08:00", "09:00", "16:31",
"12:00", "13:14", "07:00"),
To = c("07:45", "16:30", "16:15", "16:45", "00:00",
"13:15", "16:31", "12:00"),
Location = c("No Location","Kahana", "Waikiki", "Kahana",
"No Location", "Waikiki", "Kahana", "Honolulu")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.