2013-10-04 99 views
22

将excel表格导入为ArcGIS中的xy点时,我将继续失去每个点的正确DateTime标记。因此,我格式化了DateTime序列号,创建了.shp,并使用readOGR()将.shp读入R中。将excel日期时间序列号转换为R日期时间

一旦进入R,我可以使用as.Date()origin = "1899-12-30"参数将其转换为正确的日期,但时间会被忽略。虽然我看到了一个单独的Date的例子,但我还没有看到过使用DateTime的例子。我一直在使用as.Date()以及as.POSIXct(),但这看似简单的任务有点令人沮丧,因此后...

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

*感谢理查德和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小时时钟(即不是AM/PM)。我正在使用Windows 7,拥有最新的R和ArcGIS 10.

下面的代码获取正确的日期,但时间仍然缺失。

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

在此先感谢!

+1

你可以看看类似的主题[这里](http://stackoverflow.com/questions/4868693/convert-fraction -of-day-to-posix-times-in-r)和[here](http://stackoverflow.com/questions/12161984/how-to-elegantly-convert-datetime-from-decimal-to-dmy- HMS /)。 – Henrik

回答

34

您的号码正在计算天数。转换为秒,就这么简单(少了舍入误差)

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 
+0

感谢里卡多萨波塔和thelatemail提供的信息性建议 –

9

仍然存在,它只是不显示时间数据 - 见:

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 
2

使用函数ConvertToDateTime。这很简单。这里有一个例子:

convertToDateTime(helpData $ ExcelNum,产地= “1900-01-01”)

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

2

这里是另一种方式使用看门人和tibble包做到这一点:

install.packages("janitor") 

install.packages("tibble") 

library(tibble) 

library(janitor) 

excel_numeric_to_date(as.numeric(as.character(helpData$ExcelNum), date_system = "modern") 
相关问题