2

我知道之前已经在这里提出并回答了这个问题,但我没有得到提供的解决方案来为我工作。与 OP 相同,我正在将 SQL 的输出读入 R,它会自动将 Date 列识别为factor,如下所示:

Sample.Time..Trend.2.: Factor w/ 101 levels "","2013/10/24 00:19:00",..: 2 3 4 5 6 7 8 9 10 11 ...

根据原始解决方案,我已经使用该函数将因子转换为日期as.Date,但没有成功:

as.Date(df[1], format = "%Y/%m/%d %H:%M:%S")

我在这里错过了什么吗?

编辑:可重现的代码(dput(df)

structure(list(Sample.Time..Trend.1. = structure(c(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, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 
58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 
71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 
84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 
97L, 98L, 99L, 100L, 101L, 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, 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "2013/10/24 00:19:00", 
"2013/10/24 00:49:00", "2013/10/24 01:18:59", "2013/10/24 01:48:59", 
"2013/10/24 02:18:59", "2013/10/24 02:48:59", "2013/10/24 03:18:59", 
"2013/10/24 03:48:59", "2013/10/24 04:18:58", "2013/10/24 04:48:58", 
"2013/10/24 05:18:57", "2013/10/24 05:48:57", "2013/10/24 06:18:57", 
"2013/10/24 06:48:57", "2013/10/24 07:18:58", "2013/10/24 07:48:58", 
"2013/10/24 08:18:57", "2013/10/24 08:48:57", "2013/10/24 09:18:57", 
"2013/10/24 09:48:57", "2013/10/24 10:18:57", "2013/10/24 10:48:57", 
"2013/10/24 11:18:57", "2013/10/24 11:48:57", "2013/10/24 12:18:57", 
"2013/10/24 12:48:57", "2013/10/24 13:18:56", "2013/10/24 13:48:56", 
"2013/10/24 14:18:57", "2013/10/24 14:48:57", "2013/10/24 15:18:56", 
"2013/10/24 15:48:56", "2013/10/24 16:18:56", "2013/10/24 16:48:56", 
"2013/10/24 17:18:56", "2013/10/24 17:48:56", "2013/10/24 18:18:56", 
"2013/10/24 18:48:56", "2013/10/24 19:18:55", "2013/10/24 19:48:55", 
"2013/10/24 20:18:55", "2013/10/24 20:48:55", "2013/10/24 21:18:54", 
"2013/10/24 21:48:54", "2013/10/24 22:18:55", "2013/10/24 22:48:55", 
"2013/10/24 23:18:55", "2013/10/24 23:48:55", "2013/10/25 00:18:54", 
"2013/10/25 00:48:54", "2013/10/25 01:18:53", "2013/10/25 01:48:53", 
"2013/10/25 02:18:53", "2013/10/25 02:48:53", "2013/10/25 03:18:53", 
"2013/10/25 03:48:53", "2013/10/25 04:18:53", "2013/10/25 04:48:53", 
"2013/10/25 05:18:53", "2013/10/25 05:48:53", "2013/10/25 06:18:53", 
"2013/10/25 06:48:53", "2013/10/25 07:18:53", "2013/10/25 07:48:53", 
"2013/10/25 08:18:52", "2013/10/25 08:48:52", "2013/10/25 09:18:52", 
"2013/10/25 09:48:52", "2013/10/25 10:18:51", "2013/10/25 10:48:51", 
"2013/10/25 11:18:51", "2013/10/25 11:48:51", "2013/10/25 12:18:52", 
"2013/10/25 12:48:52", "2013/10/25 13:18:52", "2013/10/25 13:48:52", 
"2013/10/25 14:18:52", "2013/10/25 14:48:52", "2013/10/25 15:18:51", 
"2013/10/25 15:48:51", "2013/10/25 16:18:51", "2013/10/25 16:48:51", 
"2013/10/25 17:18:51", "2013/10/25 17:48:51", "2013/10/25 18:18:51", 
"2013/10/25 18:48:51", "2013/10/25 19:18:51", "2013/10/25 19:48:51", 
"2013/10/25 20:18:51", "2013/10/25 20:48:51", "2013/10/25 21:18:51", 
"2013/10/25 21:48:51", "2013/10/25 22:18:50", "2013/10/25 22:48:50", 
"2013/10/25 23:18:50", "2013/10/25 23:48:50", "2013/10/26 00:18:49", 
"2013/10/26 00:48:49", "2013/10/26 01:18:49", "2013/10/26 01:48:49"
), class = "factor"), AHU.DJ_SATemp = c(23.5765, 23.5814, 23.5814, 
23.5814, 23.5814, 23.5814, 23.5814, 23.5814, 23.5814, 23.5814, 
23.5814, 23.5814, 23.5814, 23.5814, 23.5814, 23.5814, 22.92, 
19.0991, 18.7197, 17.9586, 19.1917, 19.3438, 18.3791, 17.2845, 
18.8174, 17.2029, 16.2401, 18.6221, 16.6308, 20.3029, 17.1108, 
16.4354, 16.4104, 18.8174, 17.4799, 17.8511, 17.5776, 17.3822, 
18.9923, 17.9586, 18.7197, 19.3769, 20.0901, 20.5982, 20.8141, 
21.006, 21.0896, 21.1019, 21.1963, 21.1963, 21.1963, 21.1963, 
21.1963, 21.1963, 21.1963, 21.1963, 21.1963, 21.1963, 21.1963, 
21.1963, 21.1963, 21.1963, 21.1813, 21.1728, 20.8275, 18.3291, 
17.9965, 18.3105, 17.391, 16.1228, 17.1886, 16.1424, 16.2401, 
16.6308, 18.5116, 18.8174, 16.7254, 17.5142, 19.2843, 16.5331, 
17.265, 17.3731, 20.5269, 20.91, 21.1775, 21.2907, 21.2907, 21.2907, 
21.2907, 21.2907, 21.2314, 21.1963, 21.1963, 21.1171, 21.1019, 
21.0642, 21.006, 21.006, 20.9973, 20.958, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Sample.Time..Trend.1.", 
"AHU.DJ_SATemp"), row.names = c(NA, -200L), class = "data.frame")

转换方法的结果:

as.Date(df[1], format = "%Y/%m/%d %H:%M:%S")

as.Date.default(df 1 , format = "%Y/%m/%d %H:%M:%S") 中的错误:不知道如何将“df 1 ”转换为“日期”类

as.Date(as.character(df[1],"%Y/%m/%d %H:%M:%S"))

charToDate(x) 中的错误:字符串不是标准的明确格式

as.Date(factor(df[1]), format = "%Y/%m/%d %H:%M:%S")

sort.list(y) 中的错误:对于 'sort.list','x' 必须是原子的 你是否在列表中调用了 'sort'?

4

2 回答 2

5

请注意,您的错误与它无关,它应该as.Date(·)与miss indexig有关,然后您可以使用df[1]df[, 1]as.Date(·)

as.Date(df[, 1], format = "%Y/%m/%d %H:%M:%S")

看一眼?"["

于 2013-10-29T16:10:51.097 回答
-1

自动将日期转换为所需格式的功能。原始日期是因子数据类型。

date_conv=function(date)
{
  mydate = date
  mydate = as.Date(mydate, format = "%d-%B-%Y") #changes format 16-Mar-17 to 
  0017-03-16
  new_date=format(mydate, "20%y-%m-%d") #0017-03-16 to 2017-03-16
  return(new_date)
}
于 2017-05-30T06:35:17.300 回答