关于 r 中的时间匹配

About chron time matching in r

我尝试使用 r 中的时间列来匹配两个文件。因此,我选择 chron time 来实现这一点。但是当我尝试将 'Time' 列转换为 chron 时间时,我收到了 'In convert.dates(dates., format = format[[1]], origin. = origin.): 331 months out of range set to NA' 的警告消息 我的数据如下,

  Year Month Day Rain_mm                Time           chrontime
1   2019     1   1     0.6 01/01/2019 01:10:00 (01/01/19 01:10:00)
2   2019     1   2     0.0 02/01/2019 01:10:00 (02/01/19 01:10:00)
3   2019     1   3     0.0 03/01/2019 01:10:00 (03/01/19 01:10:00)
4   2019     1   4     0.0 04/01/2019 01:10:00 (04/01/19 01:10:00)
5   2019     1   5     0.0 05/01/2019 01:10:00 (05/01/19 01:10:00)
6   2019     1   6     0.0 06/01/2019 01:10:00 (06/01/19 01:10:00)
7   2019     1   7     3.4 07/01/2019 01:10:00 (07/01/19 01:10:00)
8   2019     1   8     5.8 08/01/2019 01:10:00 (08/01/19 01:10:00)
9   2019     1   9     0.4 09/01/2019 01:10:00 (09/01/19 01:10:00)
10  2019     1  10     0.0 10/01/2019 01:10:00 (10/01/19 01:10:00)
11  2019     1  11     0.2 11/01/2019 01:10:00 (11/01/19 01:10:00)
12  2019     1  12     2.8 12/01/2019 01:10:00 (12/01/19 01:10:00)
13  2019     1  13     2.6 13/01/2019 01:10:00             (NA NA)
14  2019     1  14     2.2 14/01/2019 01:10:00             (NA NA)
15  2019     1  15     3.2 15/01/2019 01:10:00             (NA NA)
16  2019     1  16     6.2 16/01/2019 01:10:00             (NA NA)
17  2019     1  17    12.6 17/01/2019 01:10:00             (NA NA)
18  2019     1  18     0.0 18/01/2019 01:10:00             (NA NA)
19  2019     1  19     0.0 19/01/2019 01:10:00             (NA NA)
20  2019     1  20     0.0 20/01/2019 01:10:00             (NA NA)
21  2019     1  21     7.4 21/01/2019 01:10:00             (NA NA)
22  2019     1  22     0.2 22/01/2019 01:10:00             (NA NA)
23  2019     1  23     0.0 23/01/2019 01:10:00             (NA NA)
24  2019     1  24     0.0 24/01/2019 01:10:00             (NA NA)
25  2019     1  25     0.0 25/01/2019 01:10:00             (NA NA)
26  2019     1  26     0.6 26/01/2019 01:10:00             (NA NA)
27  2019     1  27     7.6 27/01/2019 01:10:00             (NA NA)
28  2019     1  28     1.6 28/01/2019 01:10:00             (NA NA)
29  2019     1  29     0.0 29/01/2019 01:10:00             (NA NA)
30  2019     1  30     0.0 30/01/2019 01:10:00             (NA NA)
31  2019     1  31     0.0 31/01/2019 01:10:00             (NA NA)
32  2019     2   1     0.0 01/02/2019 01:10:00 (01/02/19 01:10:00)
33  2019     2   2     0.6 02/02/2019 01:10:00 (02/02/19 01:10:00)
34  2019     2   3     0.0 03/02/2019 01:10:00 (03/02/19 01:10:00)
35  2019     2   4     1.0 04/02/2019 01:10:00 (04/02/19 01:10:00)
36  2019     2   5     0.2 05/02/2019 01:10:00 (05/02/19 01:10:00)
37  2019     2   6     3.6 06/02/2019 01:10:00 (06/02/19 01:10:00)
38  2019     2   7     1.8 07/02/2019 01:10:00 (07/02/19 01:10:00)
39  2019     2   8     3.0 08/02/2019 01:10:00 (08/02/19 01:10:00)
40  2019     2   9     6.8 09/02/2019 01:10:00 (09/02/19 01:10:00)
41  2019     2  10     3.4 10/02/2019 01:10:00 (10/02/19 01:10:00)
42  2019     2  11     0.6 11/02/2019 01:10:00 (11/02/19 01:10:00)
43  2019     2  12     0.0 12/02/2019 01:10:00 (12/02/19 01:10:00)
44  2019     2  13     0.0 13/02/2019 01:10:00             (NA NA)
45  2019     2  14     0.0 14/02/2019 01:10:00             (NA NA)
46  2019     2  15     0.0 15/02/2019 01:10:00             (NA NA)
......

我的代码是:

Dts <- with(DMIdata, paste(substring(Time, 1,2), substring(Time,4,5), substring(Time, 9, 10), sep='/'))    
hrs <- with(DMIdata, substring(Time, 13, 20))
DMIdata$chrontime <- chron(Dts,hrs)

我想知道为什么在 chronic 转换中 13 号之后的所有日期都设置为 NA?以及如何改进我的代码以使其正常工作?

感谢您的帮助!

library(chron)

substring 的问题是订单 chron 期望 format?chron

中指定的日期是确定的

format - The dates format can be any permutation of the characters "d", "m", or "y" delimited by a separator (possibly null), e.g., "m/d/y", "d-m-y", "ymd", are all valid; the format can also be permutations of the words "day", "month" and "year" (with non-null separator), which produces the month name, e.g., "month day year" produces "April 20 1992", "day mon year" produces "20 Apr 1992".

默认情况下,如果我们查看Usage,它是mdy

chron(dates., times., format = c(dates = "m/d/y", times = "h:m:s"), out.format, origin.)

而 OP 的 Dts 在 d/m/y。我们可以在这里指定format

chron(Dts,hrs, format = c(dates = "d/m/y", times = "h:m:s"))
#[1] (01/01/19 01:10:00) (02/01/19 01:10:00) (03/01/19 01:10:00) (04/01/19 01:10:00) (05/01/19 01:10:00) (06/01/19 01:10:00)
#[7] (07/01/19 01:10:00) (08/01/19 01:10:00) (09/01/19 01:10:00) (10/01/19 01:10:00) (11/01/19 01:10:00) (12/01/19 01:10:00)
#[13] (13/01/19 01:10:00) (14/01/19 01:10:00) (15/01/19 01:10:00) (16/01/19 01:10:00) (17/01/19 01:10:00) (18/01/19 01:10:00)
#[19] (19/01/19 01:10:00) (20/01/19 01:10:00) (21/01/19 01:10:00) (22/01/19 01:10:00) (23/01/19 01:10:00) (24/01/19 01:10:00)
#[25] (25/01/19 01:10:00) (26/01/19 01:10:00) (27/01/19 01:10:00) (28/01/19 01:10:00) (29/01/19 01:10:00) (30/01/19 01:10:00)
#[31] (31/01/19 01:10:00) (01/02/19 01:10:00) (02/02/19 01:10:00) (03/02/19 01:10:00) (04/02/19 01:10:00) (05/02/19 01:10:00)
#[37] (06/02/19 01:10:00) (07/02/19 01:10:00) (08/02/19 01:10:00) (09/02/19 01:10:00) (10/02/19 01:10:00) (11/02/19 01:10:00)
#[43] (12/02/19 01:10:00) (13/02/19 01:10:00) (14/02/19 01:10:00) (15/02/19 01:10:00)

数据

DMIdata <- structure(list(Year = c(2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L), Rain_mm = c(0.6, 0, 0, 0, 0, 0, 3.4, 5.8, 0.4, 0, 0.2, 
2.8, 2.6, 2.2, 3.2, 6.2, 12.6, 0, 0, 0, 7.4, 0.2, 0, 0, 0, 0.6, 
7.6, 1.6, 0, 0, 0, 0, 0.6, 0, 1, 0.2, 3.6, 1.8, 3, 6.8, 3.4, 
0.6, 0, 0, 0, 0), Time = c("01/01/2019 01:10:00", "02/01/2019 01:10:00", 
"03/01/2019 01:10:00", "04/01/2019 01:10:00", "05/01/2019 01:10:00", 
"06/01/2019 01:10:00", "07/01/2019 01:10:00", "08/01/2019 01:10:00", 
"09/01/2019 01:10:00", "10/01/2019 01:10:00", "11/01/2019 01:10:00", 
"12/01/2019 01:10:00", "13/01/2019 01:10:00", "14/01/2019 01:10:00", 
"15/01/2019 01:10:00", "16/01/2019 01:10:00", "17/01/2019 01:10:00", 
"18/01/2019 01:10:00", "19/01/2019 01:10:00", "20/01/2019 01:10:00", 
"21/01/2019 01:10:00", "22/01/2019 01:10:00", "23/01/2019 01:10:00", 
"24/01/2019 01:10:00", "25/01/2019 01:10:00", "26/01/2019 01:10:00", 
"27/01/2019 01:10:00", "28/01/2019 01:10:00", "29/01/2019 01:10:00", 
"30/01/2019 01:10:00", "31/01/2019 01:10:00", "01/02/2019 01:10:00", 
"02/02/2019 01:10:00", "03/02/2019 01:10:00", "04/02/2019 01:10:00", 
"05/02/2019 01:10:00", "06/02/2019 01:10:00", "07/02/2019 01:10:00", 
"08/02/2019 01:10:00", "09/02/2019 01:10:00", "10/02/2019 01:10:00", 
"11/02/2019 01:10:00", "12/02/2019 01:10:00", "13/02/2019 01:10:00", 
"14/02/2019 01:10:00", "15/02/2019 01:10:00")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46"
))