我试图评估一行的时间元素来创建一个TRUE / FALSE列。例如:
datetime
10/10/15 0:45
10/1/15 0:45
10/10/15 0:30
10/1/15 0:30
10/10/15 0:15
10/1/15 0:15
10/10/15 0:00
10/1/15 0:00
10/10/15 10:45
10/10/15 1:45
10/10/15 10:30
10/10/15 1:30
10/10/15 10:15
10/10/15 1:15
10/10/15 11:45
10/10/15 11:30
10/10/15 11:15
10/10/15 11:00
10/10/15 12:45
10/10/15 12:30
10/10/15 12:15
10/10/15 12:00
10/10/15 13:45
10/10/15 13:30
10/10/15 13:15
10/10/15 14:45
10/10/15 14:30
10/10/15 14:15
10/10/15 14:00
10/10/15 15:45
10/10/15 15:30
10/10/15 15:15
10/10/15 15:00
10/10/15 16:45
10/10/15 16:30
10/10/15 16:15
10/10/15 17:45
10/10/15 17:30
10/10/15 17:15
10/10/15 17:00
10/10/15 18:45
10/10/15 18:30
10/10/15 18:15
10/10/15 18:00
10/10/15 19:45
10/10/15 19:30
10/10/15 19:15
10/10/15 20:45
10/10/15 2:45
10/10/15 20:30
10/10/15 2:30
10/10/15 20:15
10/10/15 2:15
10/10/15 20:00
10/10/15 2:00
10/10/15 21:45
10/10/15 21:30
10/10/15 21:15
10/10/15 21:00
10/10/15 22:45
10/10/15 22:30
10/10/15 22:15
10/10/15 23:45
10/10/15 23:30
10/10/15 23:15
10/10/15 23:00
10/10/15 3:45
10/10/15 3:30
10/10/15 3:15
10/10/15 3:00
10/10/15 4:45
10/10/15 4:30
10/10/15 4:15
10/10/15 5:45
10/10/15 5:30
10/10/15 5:15
10/10/15 5:00
10/10/15 6:45
10/10/15 6:30
10/10/15 6:15
10/10/15 6:00
10/10/15 7:45
10/10/15 7:30
10/10/15 7:15
10/10/15 8:45
10/10/15 8:30
10/10/15 8:15
10/10/15 8:00
10/10/15 9:45
10/10/15 9:30
10/10/15 9:15
10/10/15 9:00
10/1/15 10:45
10/1/15 1:45
10/1/15 10:30
10/1/15 1:30
10/1/15 10:15
10/1/15 1:15
10/1/15 10:00
10/11/15 10:45
假设我想要一个变量:
v1 = TRUE if 7:45<time(datetime)<8:15
AND v1 = TRUE if 23:45<time(datetime)<0:15 (next day)
ELSE = FALSE
基本上,我正在寻找类似于dplyr::mutate(between, a, b)
所做的事情,但这可以理解时间。
我的数据存储如下:
$ datetime : POSIXct, format: "2015-10-10 00:45:00" "2015-10-01 00:45:00" "2015-10-10 00:30:00" "2015-10-01 00:30:00" ...
$ year : int 2015 2015 2015 2015 2015 2015 2015 2015 2015 2015 ...
$ month : int 10 10 10 10 10 10 10 10 10 10 ...
$ day : int 10 1 10 1 10 1 10 1 10 10 ...
$ minute : int 45 45 30 30 14 14 0 0 44 44 ...
答案 0 :(得分:1)
如果您的数据位于名为test.csv
library(data.table)
library(fasttime)
data = fread('test.csv',header = TRUE,sep='\n')
data[,datetime:=fastPOSIXct(datetime)]
data[,v1:=FALSE][as.ITime(datetime) %between% as.ITime(c('01:45','08:45')) | as.ITime(datetime) %between% as.ITime(c('23:45','23:59')), v1:=TRUE]
您需要根据需要调整时间
datetime v1
1: 2010-10-15 01:45:00 TRUE
2: 2010-01-15 00:45:00 FALSE
3: 2010-10-15 01:30:00 FALSE
4: 2010-01-15 00:30:00 FALSE
...
答案 1 :(得分:1)
如果您想比较时间而不是相应的日期,您可以使用strftime
后跟as.POSIXct
将日期时间全部转换为一个日期(即今天的日期)然后比较时间。使用dplyr
:
library(dplyr)
result <- df %>% mutate(hm = as.POSIXct(strftime(datetime, format="%H:%M"),format="%H:%M"),
v1 = ifelse((as.POSIXct("7:45", format="%H:%M") < hm & hm < as.POSIXct("8:15", format="%H:%M")) |
(as.POSIXct("23:45", format="%H:%M") < hm & hm < as.POSIXct("0:15", format="%H:%M")),
TRUE, FALSE)) %>%
select(-hm)
首先创建一个列hm
,该列是datetime
的时间,但日期设置为今天。然后根据您的条件使用ifelse
来比较范围内的时间。使用您提供的数据(仅datetime
列作为数据框df
):
print(result)
## datetime v1
##1 2015-10-10 00:45:00 FALSE
##2 2015-10-01 00:45:00 FALSE
##3 2015-10-10 00:30:00 FALSE
##4 2015-10-01 00:30:00 FALSE
##5 2015-10-10 00:15:00 FALSE
##6 2015-10-01 00:15:00 FALSE
##7 2015-10-10 00:00:00 FALSE
##8 2015-10-01 00:00:00 FALSE
##9 2015-10-10 10:45:00 FALSE
##10 2015-10-10 01:45:00 FALSE
##11 2015-10-10 10:30:00 FALSE
##12 2015-10-10 01:30:00 FALSE
##13 2015-10-10 10:15:00 FALSE
##14 2015-10-10 01:15:00 FALSE
##15 2015-10-10 11:45:00 FALSE
##16 2015-10-10 11:30:00 FALSE
##17 2015-10-10 11:15:00 FALSE
##18 2015-10-10 11:00:00 FALSE
##19 2015-10-10 12:45:00 FALSE
##20 2015-10-10 12:30:00 FALSE
##21 2015-10-10 12:15:00 FALSE
##22 2015-10-10 12:00:00 FALSE
##23 2015-10-10 13:45:00 FALSE
##24 2015-10-10 13:30:00 FALSE
##25 2015-10-10 13:15:00 FALSE
##26 2015-10-10 14:45:00 FALSE
##27 2015-10-10 14:30:00 FALSE
##28 2015-10-10 14:15:00 FALSE
##29 2015-10-10 14:00:00 FALSE
##30 2015-10-10 15:45:00 FALSE
##31 2015-10-10 15:30:00 FALSE
##32 2015-10-10 15:15:00 FALSE
##33 2015-10-10 15:00:00 FALSE
##34 2015-10-10 16:45:00 FALSE
##35 2015-10-10 16:30:00 FALSE
##36 2015-10-10 16:15:00 FALSE
##37 2015-10-10 17:45:00 FALSE
##38 2015-10-10 17:30:00 FALSE
##39 2015-10-10 17:15:00 FALSE
##40 2015-10-10 17:00:00 FALSE
##41 2015-10-10 18:45:00 FALSE
##42 2015-10-10 18:30:00 FALSE
##43 2015-10-10 18:15:00 FALSE
##44 2015-10-10 18:00:00 FALSE
##45 2015-10-10 19:45:00 FALSE
##46 2015-10-10 19:30:00 FALSE
##47 2015-10-10 19:15:00 FALSE
##48 2015-10-10 20:45:00 FALSE
##49 2015-10-10 02:45:00 FALSE
##50 2015-10-10 20:30:00 FALSE
##51 2015-10-10 02:30:00 FALSE
##52 2015-10-10 20:15:00 FALSE
##53 2015-10-10 02:15:00 FALSE
##54 2015-10-10 20:00:00 FALSE
##55 2015-10-10 02:00:00 FALSE
##56 2015-10-10 21:45:00 FALSE
##57 2015-10-10 21:30:00 FALSE
##58 2015-10-10 21:15:00 FALSE
##59 2015-10-10 21:00:00 FALSE
##60 2015-10-10 22:45:00 FALSE
##61 2015-10-10 22:30:00 FALSE
##62 2015-10-10 22:15:00 FALSE
##63 2015-10-10 23:45:00 FALSE
##64 2015-10-10 23:30:00 FALSE
##65 2015-10-10 23:15:00 FALSE
##66 2015-10-10 23:00:00 FALSE
##67 2015-10-10 03:45:00 FALSE
##68 2015-10-10 03:30:00 FALSE
##69 2015-10-10 03:15:00 FALSE
##70 2015-10-10 03:00:00 FALSE
##71 2015-10-10 04:45:00 FALSE
##72 2015-10-10 04:30:00 FALSE
##73 2015-10-10 04:15:00 FALSE
##74 2015-10-10 05:45:00 FALSE
##75 2015-10-10 05:30:00 FALSE
##76 2015-10-10 05:15:00 FALSE
##77 2015-10-10 05:00:00 FALSE
##78 2015-10-10 06:45:00 FALSE
##79 2015-10-10 06:30:00 FALSE
##80 2015-10-10 06:15:00 FALSE
##81 2015-10-10 06:00:00 FALSE
##82 2015-10-10 07:45:00 FALSE
##83 2015-10-10 07:30:00 FALSE
##84 2015-10-10 07:15:00 FALSE
##85 2015-10-10 08:45:00 FALSE
##86 2015-10-10 08:30:00 FALSE
##87 2015-10-10 08:15:00 FALSE
##88 2015-10-10 08:00:00 TRUE
##89 2015-10-10 09:45:00 FALSE
##90 2015-10-10 09:30:00 FALSE
##91 2015-10-10 09:15:00 FALSE
##92 2015-10-10 09:00:00 FALSE
##93 2015-10-01 10:45:00 FALSE
##94 2015-10-01 01:45:00 FALSE
##95 2015-10-01 10:30:00 FALSE
##96 2015-10-01 01:30:00 FALSE
##97 2015-10-01 10:15:00 FALSE
##98 2015-10-01 01:15:00 FALSE
##99 2015-10-01 10:00:00 FALSE
##100 2015-10-11 10:45:00 FALSE