向不同时区的DateTime值添加小时数

时间:2014-07-31 23:07:08

标签: r datetime

虽然我在R中发现了一些关于POSIXct和其他时间函数的信息性帖子,但我仍然在努力进行多个时区的时间转换。

我的时间存储为excel文件中的数字,格式为GMT,包含在下面。

Data <- structure(list(DateTime = c(40361.584526, 40361.667836, 40361.751285, 
40361.834421, 40361.917986, 40362.000972, 40362.084479, 40362.167396, 
40362.252257, 40362.335301, 40362.417211, 40362.500926, 40362.585185, 
40362.668796, 40362.751655, 40362.834664, 40362.917847, 40363.001007, 
40363.084641, 40363.1675, 40363.251123, 40363.334803, 40363.417616, 
40363.502095, 40363.584514, 40363.667639, 40363.750625, 40363.834225, 
40363.917743, 40354.335405, 40737.501782, 40603.917292, 40604.000556, 
40604.083808, 40604.167292, 40604.250579, 40604.33375, 40604.417164, 
40604.500625, 40604.584074, 40604.666968, 40604.750613, 40604.834306, 
40604.917211, 40605.000625, 40605.083958, 40605.167245, 40605.250567, 
40605.333924, 40605.417488, 40605.500694, 40605.584306, 40605.66728, 
40605.750718, 40605.834236, 40605.917905, 40606.000625, 40606.083901, 
40606.167292, 40606.250509, 41271.500544, 40048.917037, 40049, 
40049.085926, 40049.583704, 40049.666667, 40049.74963, 40049.832593, 
40051.666667, 40051.752593, 40051.835556, 40051.918519, 40052.001482, 
40052.167407, 40052.25037, 40052.333333, 40052.416296, 40052.751111, 
40052.834074, 40052.917037, 40053, 40053.082963, 40053.168889, 
40053.251852, 40053.334815, 40053.417778, 40053.500741, 40053.583704, 
40053.666667, 40054.084444), StudyArea = structure(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, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Cali", "Colo", "Pata"
), class = "factor")), .Names = c("DateTime", "StudyArea"), row.names = c(NA, 
-90L), class = "data.frame")

负责人在这里:

 > head(data)
  DateTime StudyArea
1 40361.58      Cali
2 40361.67      Cali
3 40361.75      Cali
4 40361.83      Cali
5 40361.92      Cali
6 40362.00      Cali

我首先使用代码

将DateTime更改为GMT对象中的POSIXct
Data$TimeGMT <- as.POSIXct(Data$DateTime * (60*60*24), origin="1899-12-30", tz="GMT") 

我的目标是分别从Cali,Colo和Pata的TimeGMT中减去8,7和4小时。我的工作代码如下,但导致明显不正确的本地日期。

LocalDateTime <- as.POSIXct(ifelse(Data$StudyArea == "Cali", Data$TimeGMT - 8*60*60,
    ifelse(Data$StudyArea == "Colo", Data$TimeGMT - 7*60*60,
    Data$TimeGMT  - 4*60*60)),origin="1899-12-30")

我怀疑有一种更简单的方法可以做到这一点并感谢任何有关编码的帮助或者在哪里找到R时区格式的列表,因为我无法在timezone()帮助文件中找到它。 / p>

2 个答案:

答案 0 :(得分:3)

应该可以避免使用凌乱的ifelse - 嵌套,方法是选择一个整数来乘以那些&#34;小时&#34;由:

LocalDateTime <- Data$TimeGMT - 60*60*c(8,7,4)[
                           match(Data$StudyArea , c( "Cali", "Colo","Pata")  )

答案 1 :(得分:1)

这应该有效

LocalDateTime <- as.POSIXct(ifelse(Data$StudyArea == "Cali", Data$TimeGMT - 8*60*60,
    ifelse(Data$StudyArea == "Colo", Data$TimeGMT - 7*60*60,
    Data$TimeGMT  - 4*60*60)), origin="1970-01-01", tz="GMT")

请注意,我们在此处使用标准POSIX原点。这是因为您的日期已经从excel版本转换为POSIX值。所以Excel起源并不重要。我们还需要将时区保持在GMT,否则默认是调整您的本地tz。这样做会给出

head(cbind(Data, LocalDateTime))

  DateTime StudyArea             TimeGMT       LocalDateTime
1 40361.58      Cali 2010-07-02 14:01:43 2010-07-02 06:01:43
2 40361.67      Cali 2010-07-02 16:01:41 2010-07-02 08:01:41
3 40361.75      Cali 2010-07-02 18:01:51 2010-07-02 10:01:51
4 40361.83      Cali 2010-07-02 20:01:33 2010-07-02 12:01:33
5 40361.92      Cali 2010-07-02 22:01:53 2010-07-02 14:01:53
6 40362.00      Cali 2010-07-03 00:01:23 2010-07-02 16:01:23

更简单的方法是

LocalDateTime <- Data$TimeGMT - ifelse(Data$StudyArea == "Cali", 8*60*60,
    ifelse(Data$StudyArea == "Colo", 7*60*60, 4*60*60))

这样您就不必担心转换,因为ifelse()不会放弃课程