我有时间数组;
time_array
[1] 08:05:02 08:46:08 09:13:54 09:51:21 10:07:31 11:34:12
[7] 11:45:28 12:18:14 12:26:05 12:58:35 13:11:09 14:14:29
[13] 15:28:47 15:56:47 16:22:06 16:40:15 18:15:00 19:01:39
[19] 20:36:22 21:16:27 21:26:11 21:43:06 23:51:47 00:18:35
[25] 01:02:37 02:08:14 02:52:09 04:02:01 04:37:46 05:41:58
[31] 05:59:22 07:22:07 08:32:47 09:13:23 09:39:01 10:19:35
[37] 11:53:18 12:05:53 12:18:42 12:33:04 13:16:19 13:37:34
[43] 13:54:14 14:31:39 14:44:46 15:26:23 16:03:25 17:21:44
[49] 18:00:24 19:10:50 19:44:01 20:55:16 21:09:06 22:02:01
[55] 22:53:00 23:19:43 23:59:01 01:39:20 02:28:35 02:41:08
[61] 02:57:42 03:03:45 03:10:13 04:57:44 06:00:59 07:51:50
[67] 08:10:19 08:23:24 08:32:52 09:37:47 10:43:16 11:17:07
我需要对这些值进行一些控制。例如;
if(time_array[i]>"08:05:00" | time_array[i]<"08:15:00")){
cond[i]<-1
}else{
cond[i]<-0
}
我该怎么做?
答案 0 :(得分:1)
我们可以在基础R中做这样的事情:
# Convert to POSIXct
times <- as.POSIXct(time_array, format = "%H:%M:%S")
# Store in data.frame and flag entries
df <- data.frame(
time = time_array,
flag = as.numeric(
times > as.POSIXct("08:05:00", format = "%H:%M:%S") &
times < as.POSIXct("08:15:00", format = "%H:%M:%S")))
head(df);
# time flag
#1 08:05:02 1
#2 08:46:08 0
#3 09:13:54 0
#4 09:51:21 0
#5 10:07:31 0
#6 11:34:12 0
time_array <- c(
'08:05:02', '08:46:08', '09:13:54', '09:51:21', '10:07:31', '11:34:12',
'11:45:28', '12:18:14', '12:26:05', '12:58:35', '13:11:09', '14:14:29',
'15:28:47', '15:56:47', '16:22:06', '16:40:15', '18:15:00', '19:01:39',
'20:36:22', '21:16:27', '21:26:11', '21:43:06', '23:51:47', '00:18:35',
'01:02:37', '02:08:14', '02:52:09', '04:02:01', '04:37:46', '05:41:58',
'05:59:22', '07:22:07', '08:32:47', '09:13:23', '09:39:01', '10:19:35',
'11:53:18', '12:05:53', '12:18:42', '12:33:04', '13:16:19', '13:37:34',
'13:54:14', '14:31:39', '14:44:46', '15:26:23', '16:03:25', '17:21:44',
'18:00:24', '19:10:50', '19:44:01', '20:55:16', '21:09:06', '22:02:01',
'22:53:00', '23:19:43', '23:59:01', '01:39:20', '02:28:35', '02:41:08',
'02:57:42', '03:03:45', '03:10:13', '04:57:44', '06:00:59', '07:51:50',
'08:10:19', '08:23:24', '08:32:52', '09:37:47', '10:43:16', '11:17:07')