我们有什么方法可以在if else循环中给出一个条件语句
a= "06/28/2017 08:45"
b= "06/28/2017 08:32"
c= difftime(a,b,units="mins)
`if(a+30 > b)
{
print("you are late")
}else
print("on time")`
我的数据框是
col1 col2
6/27/2017 11:00
6/27/2017 20:45
6/24/2017 19:30 6/24/2017 18:48
6/27/2017 20:00 6/28/2017 3:16
6/29/2017 18:30
7/4/2017 7:30
6/24/2017 1:30 6/24/2017 0:21
6/29/2017 10:00
6/28/2017 16:30 6/28/2017 18:39
7/2/2017 19:00
我收到错误
`1.Error in if (a+30 > b) missing value where TRUE/FALSE needed
In Ops.factor(a+30 > b) :‘>’ not meaningful for factors
2: In if (a+30 > b) { : the condition has length > 1 and only the first element will be used`
我要做的是向'a'添加30分钟并与'b'进行比较以确定某些事情是迟到还是准时。
答案 0 :(得分:2)
如果您正确格式化日期并在if语句中使用减号而不是加号,那么它可以正常工作:
a= as.Date("06/28/2017 08:45", "%m/%d/%Y %H:%M")
b= as.Date("06/28/2017 08:32", "%m/%d/%Y %H:%M")
if(a-10 > b) {
print("you are late")
} else {
print("on time")
}
将a-10改为-30,你会迟到的。
答案 1 :(得分:1)
怎么样:
library(lubridate)
library(dplyr)
a= mdy_hm("06/28/2017 08:20")
b= mdy_hm("06/28/2017 08:32")
if_else(a + minutes(30) > b, "you are late", "on time")
答案 2 :(得分:0)
library(lubridate)
a=mdy_hm("06/28/2017 08:45")
b=mdy_hm("06/28/2017 08:32")
c= abs(difftime(a,b,units="mins))
d=ifelse(30>c,"you are late","on time")
print(d)