【问题标题】:Converting excel DateTime serial number to R DateTime将excel DateTime序列号转换为R DateTime
【发布时间】:2013-10-10 22:40:31
【问题描述】:

当 Excel 表在 ArcGIS 中作为 xy 点导入时,我会继续丢失每个点的正确日期时间戳。因此,我已经格式化了 DateTime 序列号,创建了 .shp,并使用 readOGR() 将 .shp 读入了 R。

在 R 中,我可以使用 as.Date()origin = "1899-12-30" 参数转换为正确的日期,但时间被忽略了。虽然我看到了具有唯一日期的示例,但我还没有看到使用 DateTime 的示例。我一直在使用as.Date()as.POSIXct(),但是这个看似简单的任务有点令人沮丧,因此发布...

我创建了一个样本数据集,其中包含 10 行正确的 DateTime 格式以及 excel 序列号。

*感谢 Richard 和 thelatemail 对早期障碍的敏锐观察。我已更正数据并在此处重新发布。

这是我的示例数据

helpData <- structure(list(ID = 1:10, DateTime = structure(c(9L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 8L), .Label = c("3/11/2011 7:55", "3/13/2011 7:55", 
"3/14/2011 0:00", "3/14/2011 10:04", "3/14/2011 7:55", "3/15/2011 19:55", 
"3/17/2011 7:55", "3/18/2011 4:04", "3/4/2011 6:00"), class = "factor"), 
ExcelNum = c(40606.25, 40613.32986, 40615.32986, 40616, 40616.41944, 
40616.32986, 40617.82986, 40619.32986, 40620.16944, 40620.16944
)), .Names = c("ID", "DateTime", "ExcelNum"), class = "data.frame", row.names = c(NA, 
-10L))

head(helpData)

日期时间是格林威治标准时间。时间是 24 小时制(即不是上午/下午)。我正在使用 Windows 7,拥有最新的 R 和 ArcGIS 10。

下面的代码得到了正确的日期,但时间仍然缺失。

newDateTime <- as.Date(helpData[ , "ExcelNum"], origin = "1899-12-30")
head(newDateTime)

提前致谢!

【问题讨论】:

  • 你可以看看herehere类似的话题。

标签: r


【解决方案1】:

还有一个变种:

library("datetimeutils")
convert_date(helpData$ExcelNum, type = "Excel")
## [1] "2011-03-04" "2011-03-11" "2011-03-13" "2011-03-14" "2011-03-14"
## [6] "2011-03-14" "2011-03-15" "2011-03-17" "2011-03-18" "2011-03-18"

convert_date(helpData$ExcelNum, type = "Excel", fraction = TRUE)
## [1] "2011-03-04 06:00:00 CET" "2011-03-11 07:54:59 CET"
## [3] "2011-03-13 07:54:59 CET" "2011-03-14 00:00:00 CET"
## [5] "2011-03-14 10:03:59 CET" "2011-03-14 07:54:59 CET"
## [7] "2011-03-15 19:54:59 CET" "2011-03-17 07:54:59 CET"
## [9] "2011-03-18 04:03:59 CET" "2011-03-18 04:03:59 CET"

【讨论】:

    【解决方案2】:

    使用函数convertToDateTime。这是直截了当的。这是一个例子:

    library(openxlsx)
    convertToDateTime(helpData$ExcelNum, origin = "1900-01-01")
    

    让我知道它是如何工作的。

    【讨论】:

      【解决方案3】:

      这是使用 janitor 和 tibble 包的另一种方法:

      install.packages("janitor")
      
      install.packages("tibble")
      
      library(tibble)
      
      library(janitor)
      
      excel_numeric_to_date(as.numeric(as.character(helpData$ExcelNum), date_system = "modern")
      

      【讨论】:

        【解决方案4】:

        时间数据还在,只是没有显示——见:

        as.numeric(newDateTime)
        #[1] 15037.25 15044.33 15046.33 15047.00 etc etc
        

        如果您希望处理部分时间,您可能最好使用POSIXct 表示。为此,您可以转换为Date,然后转换为POSIXct,但如果您想与DateTime 列进行直接比较,这确实会带来时区问题。

        helpData$newDate <- as.POSIXct(as.Date(helpData$ExcelNum,origin="1899-12-30"))
        attr(helpData$newDate,"tzone") <- "UTC"
        helpData
        
        #   ID        DateTime ExcelNum             newDate
        #1   1   3/4/2011 6:00 40606.25 2011-03-04 06:00:00
        #2   2  3/11/2011 7:55 40613.33 2011-03-11 07:54:59
        #3   3  3/13/2011 7:55 40615.33 2011-03-13 07:54:59
        #4   4  3/14/2011 0:00 40616.00 2011-03-14 00:00:00
        #5   5 3/14/2011 10:04 40616.42 2011-03-14 10:03:59
        #6   6  3/14/2011 7:55 40616.33 2011-03-14 07:54:59
        #7   7 3/15/2011 19:55 40617.83 2011-03-15 19:54:59
        #8   8  3/17/2011 7:55 40619.33 2011-03-17 07:54:59
        #9   9  3/18/2011 4:04 40620.17 2011-03-18 04:03:59
        #10 10  3/18/2011 4:04 40620.17 2011-03-18 04:03:59
        

        【讨论】:

          【解决方案5】:

          您的号码正在计算天数。转换为秒,一切就绪(减少舍入误差)

          helpData[["ExcelDate"]] <- 
            as.POSIXct(helpData[["ExcelNum"]] * (60*60*24)
              , origin="1899-12-30"
              , tz="GMT")
          
          
          #     ID        DateTime ExcelNum           ExcelDate
          #  1   1   3/4/2011 6:00 40606.25 2011-03-04 06:00:00
          #  2   2  3/11/2011 7:55 40613.33 2011-03-11 07:54:59
          #  3   3  3/13/2011 7:55 40615.33 2011-03-13 07:54:59
          #  4   4  3/14/2011 0:00 40616.00 2011-03-14 00:00:00
          #  5   5 3/14/2011 10:04 40616.42 2011-03-14 10:03:59
          #  6   6  3/14/2011 7:55 40616.33 2011-03-14 07:54:59
          #  7   7 3/15/2011 19:55 40617.83 2011-03-15 19:54:59
          #  8   8  3/17/2011 7:55 40619.33 2011-03-17 07:54:59
          #  9   9  3/18/2011 4:04 40620.17 2011-03-18 04:03:59
          #  10 10  3/18/2011 4:04 40620.17 2011-03-18 04:03:59
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-10-18
            • 2016-09-08
            • 2017-04-12
            • 1970-01-01
            • 2015-01-02
            相关资源
            最近更新 更多