当我做了时间截止时,我喜欢将截断值作为对象。这样,如果您需要更改截断值,则更改对象的值而不是条件语句中的值会更容易。
我下面的代码使用了lubridate(),这是一个很好的管理时间/日期的包。
我在下面的代码应该给你需要在分析中加入一个虚拟变量的信息。
###
### Load Package
###
library(lubridate)
###
### Designate Time Cut-Offs
###
Beginning <- hms("06:00:00")
End <- hms("20:00:00")
###
### Designate Test Cut-Offs
###
Test.1 <- hms("5:00:00")
Test.2 <- hms("11:00:00")
###
### Test Conditional Logic
###
### Value will be 1 if time is between, value will be 0 if it is not.
###
ifelse(((Test.1 >= Beginning) & (Test.1 <= End)) , 1, 0)
########## This should (and does) return a 0
ifelse(((Test.2 >= Beginning) & (Test.2 <= End)) , 1, 0)
####### This should (and does) return a 1
###
### Create New Variable On Previous Data Frame (Your.DF) named Time.Dummy
###
### Value for new variable will be 1 if time is between, value will be 0 if it is not.
###
Your.DF$Time.Dummy <- ifelse(((time.cet >= Beginning) & (time.cet <= End)) , 1, 0)
什么是你的输入?你想如何输出?你想要一个带有列日期和相应的0/1值的data.frame,或者只是0/1的矢量,因为日期已经存储在其他地方了?请提供一些数据/信息来处理... – digEmAll