在获取一列数字以转换为时间格式方面遇到一些麻烦。我想用作时间序列的参考。列以3的间隔递增,代表分钟。我想将其转换为带有或不带有日期的时间戳。我已经尝试过使用hms(df$V1
,as.POSIXct(df$V1, format=%H:%M)
和其他版本的lubridate库,使用chron(dates = NULL, times = df$V1, format = c(NULL, "m"), "h:m")
的{{3}}库,尽管我很确定我不了解正确的格式,chron使用strptime(df$V1, format="%M")
以及format(strptime(test$rn,format = "%M"), format = "%H:%M")
和strptime。其中一些我必须工作,但是当他们这样做时,我始终遇到一个示例中所示的相同问题:
library(data.table)
df<-as.data.table(seq(1,801,by=3))
strptime(df$V1, format="%M")
哪个返回:
[1] "2019-03-22 00:00:00 EDT" "2019-03-22 00:03:00 EDT"
[3] "2019-03-22 00:06:00 EDT" "2019-03-22 00:09:00 EDT"
[5] "2019-03-22 00:12:00 EDT" "2019-03-22 00:15:00 EDT"
[7] "2019-03-22 00:18:00 EDT" "2019-03-22 00:21:00 EDT"
[9] "2019-03-22 00:24:00 EDT" "2019-03-22 00:27:00 EDT"
[11] "2019-03-22 00:30:00 EDT" "2019-03-22 00:33:00 EDT"
[13] "2019-03-22 00:36:00 EDT" "2019-03-22 00:39:00 EDT"
[15] "2019-03-22 00:42:00 EDT" "2019-03-22 00:45:00 EDT"
[17] "2019-03-22 00:48:00 EDT" "2019-03-22 00:51:00 EDT"
[19] "2019-03-22 00:54:00 EDT" "2019-03-22 00:57:00 EDT"
[21] NA NA
[23] NA NA
[25] NA NA
[27] NA NA
[29] NA NA
[31] NA NA
[33] NA NA
[35] "2019-03-22 00:10:00 EDT" "2019-03-22 00:10:00 EDT"
[37] "2019-03-22 00:10:00 EDT" "2019-03-22 00:11:00 EDT"
[39] "2019-03-22 00:11:00 EDT" "2019-03-22 00:11:00 EDT"
[41] "2019-03-22 00:12:00 EDT" "2019-03-22 00:12:00 EDT"
[43] "2019-03-22 00:12:00 EDT" "2019-03-22 00:12:00 EDT"
[45] "2019-03-22 00:13:00 EDT" "2019-03-22 00:13:00 EDT"
[47] "2019-03-22 00:13:00 EDT" "2019-03-22 00:14:00 EDT"
[49] "2019-03-22 00:14:00 EDT" "2019-03-22 00:14:00 EDT"
[51] "2019-03-22 00:15:00 EDT" "2019-03-22 00:15:00 EDT"
[53] "2019-03-22 00:15:00 EDT" "2019-03-22 00:15:00 EDT"
[55] "2019-03-22 00:16:00 EDT" "2019-03-22 00:16:00 EDT"
[57] "2019-03-22 00:16:00 EDT" "2019-03-22 00:17:00 EDT"
[59] "2019-03-22 00:17:00 EDT" "2019-03-22 00:17:00 EDT"
[61] "2019-03-22 00:18:00 EDT" "2019-03-22 00:18:00 EDT"
[63] "2019-03-22 00:18:00 EDT" "2019-03-22 00:18:00 EDT"
[65] "2019-03-22 00:19:00 EDT" "2019-03-22 00:19:00 EDT"
[67] "2019-03-22 00:19:00 EDT" "2019-03-22 00:20:00 EDT"
[69] "2019-03-22 00:20:00 EDT" "2019-03-22 00:20:00 EDT"
[71] "2019-03-22 00:21:00 EDT" "2019-03-22 00:21:00 EDT"
[73] "2019-03-22 00:21:00 EDT" "2019-03-22 00:21:00 EDT"
[75] "2019-03-22 00:22:00 EDT" "2019-03-22 00:22:00 EDT"
[77] "2019-03-22 00:22:00 EDT" "2019-03-22 00:23:00 EDT"
[79] "2019-03-22 00:23:00 EDT" "2019-03-22 00:23:00 EDT"
[81] "2019-03-22 00:24:00 EDT" "2019-03-22 00:24:00 EDT"
[83] "2019-03-22 00:24:00 EDT" "2019-03-22 00:24:00 EDT"
[85] "2019-03-22 00:25:00 EDT" "2019-03-22 00:25:00 EDT"
[87] "2019-03-22 00:25:00 EDT" "2019-03-22 00:26:00 EDT"
[89] "2019-03-22 00:26:00 EDT" "2019-03-22 00:26:00 EDT"
[91] "2019-03-22 00:27:00 EDT" "2019-03-22 00:27:00 EDT"
[93] "2019-03-22 00:27:00 EDT" "2019-03-22 00:27:00 EDT"
[95] "2019-03-22 00:28:00 EDT" "2019-03-22 00:28:00 EDT"
[97] "2019-03-22 00:28:00 EDT" "2019-03-22 00:29:00 EDT"
[99] "2019-03-22 00:29:00 EDT" "2019-03-22 00:29:00 EDT"
[101] "2019-03-22 00:30:00 EDT" "2019-03-22 00:30:00 EDT"
[103] "2019-03-22 00:30:00 EDT" "2019-03-22 00:30:00 EDT"
[105] "2019-03-22 00:31:00 EDT" "2019-03-22 00:31:00 EDT"
[107] "2019-03-22 00:31:00 EDT" "2019-03-22 00:32:00 EDT"
[109] "2019-03-22 00:32:00 EDT" "2019-03-22 00:32:00 EDT"
[111] "2019-03-22 00:33:00 EDT" "2019-03-22 00:33:00 EDT"
[113] "2019-03-22 00:33:00 EDT" "2019-03-22 00:33:00 EDT"
[115] "2019-03-22 00:34:00 EDT" "2019-03-22 00:34:00 EDT"
[117] "2019-03-22 00:34:00 EDT" "2019-03-22 00:35:00 EDT"
[119] "2019-03-22 00:35:00 EDT" "2019-03-22 00:35:00 EDT"
[121] "2019-03-22 00:36:00 EDT" "2019-03-22 00:36:00 EDT"
[123] "2019-03-22 00:36:00 EDT" "2019-03-22 00:36:00 EDT"
[125] "2019-03-22 00:37:00 EDT" "2019-03-22 00:37:00 EDT"
[127] "2019-03-22 00:37:00 EDT" "2019-03-22 00:38:00 EDT"
[129] "2019-03-22 00:38:00 EDT" "2019-03-22 00:38:00 EDT"
[131] "2019-03-22 00:39:00 EDT" "2019-03-22 00:39:00 EDT"
[133] "2019-03-22 00:39:00 EDT" "2019-03-22 00:39:00 EDT"
[135] "2019-03-22 00:40:00 EDT" "2019-03-22 00:40:00 EDT"
[137] "2019-03-22 00:40:00 EDT" "2019-03-22 00:41:00 EDT"
[139] "2019-03-22 00:41:00 EDT" "2019-03-22 00:41:00 EDT"
[141] "2019-03-22 00:42:00 EDT" "2019-03-22 00:42:00 EDT"
[143] "2019-03-22 00:42:00 EDT" "2019-03-22 00:42:00 EDT"
[145] "2019-03-22 00:43:00 EDT" "2019-03-22 00:43:00 EDT"
[147] "2019-03-22 00:43:00 EDT" "2019-03-22 00:44:00 EDT"
[149] "2019-03-22 00:44:00 EDT" "2019-03-22 00:44:00 EDT"
[151] "2019-03-22 00:45:00 EDT" "2019-03-22 00:45:00 EDT"
[153] "2019-03-22 00:45:00 EDT" "2019-03-22 00:45:00 EDT"
[155] "2019-03-22 00:46:00 EDT" "2019-03-22 00:46:00 EDT"
[157] "2019-03-22 00:46:00 EDT" "2019-03-22 00:47:00 EDT"
[159] "2019-03-22 00:47:00 EDT" "2019-03-22 00:47:00 EDT"
[161] "2019-03-22 00:48:00 EDT" "2019-03-22 00:48:00 EDT"
[163] "2019-03-22 00:48:00 EDT" "2019-03-22 00:48:00 EDT"
[165] "2019-03-22 00:49:00 EDT" "2019-03-22 00:49:00 EDT"
[167] "2019-03-22 00:49:00 EDT" "2019-03-22 00:50:00 EDT"
[169] "2019-03-22 00:50:00 EDT" "2019-03-22 00:50:00 EDT"
[171] "2019-03-22 00:51:00 EDT" "2019-03-22 00:51:00 EDT"
[173] "2019-03-22 00:51:00 EDT" "2019-03-22 00:51:00 EDT"
[175] "2019-03-22 00:52:00 EDT" "2019-03-22 00:52:00 EDT"
[177] "2019-03-22 00:52:00 EDT" "2019-03-22 00:53:00 EDT"
[179] "2019-03-22 00:53:00 EDT" "2019-03-22 00:53:00 EDT"
[181] "2019-03-22 00:54:00 EDT" "2019-03-22 00:54:00 EDT"
[183] "2019-03-22 00:54:00 EDT" "2019-03-22 00:54:00 EDT"
[185] "2019-03-22 00:55:00 EDT" "2019-03-22 00:55:00 EDT"
[187] "2019-03-22 00:55:00 EDT" "2019-03-22 00:56:00 EDT"
[189] "2019-03-22 00:56:00 EDT" "2019-03-22 00:56:00 EDT"
[191] "2019-03-22 00:57:00 EDT" "2019-03-22 00:57:00 EDT"
[193] "2019-03-22 00:57:00 EDT" "2019-03-22 00:57:00 EDT"
[195] "2019-03-22 00:58:00 EDT" "2019-03-22 00:58:00 EDT"
[197] "2019-03-22 00:58:00 EDT" "2019-03-22 00:59:00 EDT"
[199] "2019-03-22 00:59:00 EDT" "2019-03-22 00:59:00 EDT"
[201] NA NA
[203] NA NA
[205] NA NA
[207] NA NA
[209] NA NA
[211] NA NA
[213] NA NA
[215] NA NA
[217] NA NA
[219] NA NA
[221] NA NA
[223] NA NA
[225] NA NA
[227] NA NA
[229] NA NA
[231] NA NA
[233] NA NA
[235] NA NA
[237] NA NA
[239] NA NA
[241] NA NA
[243] NA NA
[245] NA NA
[247] NA NA
[249] NA NA
[251] NA NA
[253] NA NA
[255] NA NA
[257] NA NA
[259] NA NA
[261] NA NA
[263] NA NA
[265] NA NA
[267] NA NA
可以看出,性能一直很好,直到达到1小时标记,然后返回NA代表多个值。然后,它恢复生成值,但是这些值每分钟返回3次,而不是每3分钟返回一次。在第200个值之后,它将完全停止生成值。此外,它在超过一个小时标记后的一小时内不会生成任何值。我工作的所有方法都存在相同的问题。任何帮助将不胜感激。
答案 0 :(得分:1)
您是否考虑过在R的seq.POSIXt
库中使用base
函数?
该函数具有以下功能:
seq.POSIXt(from, to, by, length.out = NULL, along.with = NULL, ...)
也许我不理解您的from
和to
是什么,但我相信以下方法应该可以工作。
library(data.table)
fromDate <- as.POSIXct('2018-03-22')
toDate <- as.POSIXct('2018-03-23')
df<-as.data.table(seq.POSIXt(fromDate, toDate, by = '3 min'))
将您的fromDate和toDate调整为对您有意义的任何时间。如果您只想增加固定数量的职位,我可以对此进行编辑。否则,这将非常迅速地起作用!