转换mm / dd / yyyy格式为R中的日期

时间:2018-11-17 22:13:08

标签: r

我在访问数据库中有此数据集:

Sample <- structure(list(SampleRowID = 164:173, StationCode = c("Gate 1", 
"Gate 1", "Gate 1", "Gate 1", "Gate 1", "Gate 1", "Gate 1", "Gate 1", 
"Gate 1", "Gate 1"), SampleDate = c(777427200, 777427200, 777513600, 
 777513600, 777600000, 777772800, 777859200, 777859200, 777859200, 
 777945600), SampleTime = c(-2209132440, -2209108440, -2209131900, 
-2209106280, -2209131420, -2209107120, -2209132140, -2209123680, 
-2209105800, -2209130640), MethodCode = c("RSTR", "RSTR", "RSTR", 
"RSTR", "RSTR", "RSTR", "RSTR", "RSTR", "RSTR", "RSTR"), SampleID = 
c("233_94", 
"234_94", "234_94", "235_94", "235_94", "238_94", "238_94", "239_94", 
"239_94", "239_94")), .Names = c("SampleRowID", "StationCode", 
"SampleDate", "SampleTime", "MethodCode", "SampleID"), class = "data.frame", 
row.names = c(NA, 
-10L)) 

SampleRowID StationCode SampleDate  SampleTime MethodCode SampleID
      164      Gate 1  777427200 -2209132440       RSTR   233_94
      165      Gate 1  777427200 -2209108440       RSTR   234_94
      166      Gate 1  777513600 -2209131900       RSTR   234_94
      167      Gate 1  777513600 -2209106280       RSTR   235_94
      168      Gate 1  777600000 -2209131420       RSTR   235_94
      169      Gate 1  777772800 -2209107120       RSTR   238_94
      170      Gate 1  777859200 -2209132140       RSTR   238_94
      171      Gate 1  777859200 -2209123680       RSTR   239_94
      172      Gate 1  777859200 -2209105800       RSTR   239_94
      173      Gate 1  777945600 -2209130640       RSTR   239_94

SampleDate的格式为01/04/2014,但是,当我导入R时,我的SampleDate列将转换为数值。我用过:

      Sample$SampleDate <- as.Date(Sample$SampleDate, "%m/%d/%Y")

但是它不起作用,我得到了错误:

      Error in as.Date.numeric(as.numeric(Sample$SampleDate, "%m/%d/%Y",  
      'origin' must be supplied

然后我提供了这样的原点:

       Sample$SampleDate <- as.Date(as.numeric(Sample$SampleDate, 
       "%m/%d/%Y",origin="01-01-1970"))

并收到此错误:

          Error in as.Date.numeric(as.numeric(Sample$SampleDate, "%m/%d/%Y", 
          origin = "1870-01-01")) : 
          'origin' must be supplied

原点被忽略。当我从Access导入数据集时,日期格式更改为“双精度”格式。如何转换回原始日期格式?任何帮助表示赞赏。

     UPDATE:

我的表已通过MS Access->导入到R中,然后保存到sqlite数据库中。然后从R我连接到sqlite数据库,并将'Sample'表导入R工作区。我认为sqlite正在破坏我的SampleDate格式。如果我将“示例”表另存为csv格式并将其导入R,那么as.Date效果很好。如果有人有兴趣重建我的问题,我可以向数据库提供我使用的所有R代码。我只是无法在此处复制以上内容,因为我不知道如何发布访问数据库。

1 个答案:

答案 0 :(得分:0)

   Sample$SampleDate <- as.Date(as.numeric(Sample$SampleDate, 
   "%m/%d/%Y",origin="01-01-1970"))

在这里,您在origin中而不是as.numeric中调用了as.Date参数。但这并不重要,因为输出不准确。

> as.Date(as.numeric(Sample$SampleDate, "%m/%d/%Y"), origin="01-01-1970")
 [1] "2128524-06-06" "2128524-06-06" "2128760-12-26" "2128760-12-26" "2128997-07-16" "2129470-08-26"
 [7] "2129707-03-17" "2129707-03-17" "2129707-03-17" "2129943-10-06"

您的SampleDate日期时间格式看起来像UNIX纪元,因此as.POSIXct会更合适。

> as.Date(as.POSIXct(Sample$SampleDate, origin="1970-01-01"))
 [1] "1994-08-21" "1994-08-21" "1994-08-22" "1994-08-22" "1994-08-23" "1994-08-25" "1994-08-26"
 [8] "1994-08-26" "1994-08-26" "1994-08-27"

哪个更合适。