尝试转换时区时,日期/时间无法正确转换

时间:2018-08-14 18:28:44

标签: r date data-conversion

给出以下数据集(full.merge.sub):

      calltimes.full.x timezone.recode.x
1  2018-03-27 17:36:51   America/Detroit
2  2018-04-09 15:00:22   America/Detroit
3  2018-03-27 17:51:44   America/Detroit
4  2018-04-20 20:11:48   America/Detroit
5  2018-04-09 16:22:05   America/Detroit
6  2018-03-27 18:45:16   America/Detroit
7  2018-04-09 15:04:30   America/Detroit
8  2018-03-28 14:27:58   America/Chicago
9  2018-03-28 17:33:44   America/Chicago
10 2018-03-27 17:51:44   America/Detroit

以及以下用于转换时区的函数:

forceTZ <- function(x,tz)
{
  return(as.POSIXct(format(x),tz=tz))
}

当我将函数置于循环中时,我得到的结果与单行调用函数时得到的结果不同:

for(i in 1:10){
   if(is.na(full.merge.sub$timezone.recode.x[i])==FALSE){
     full.merge.sub$calltime.converted.x[i]<-forceTZ(full.merge.sub$calltimes.full.x[i], full.merge.sub$timezone.recode.x[i])
   }
 }

 summary(full.merge.sub[1:10,])
 calltimes.full.x              timezone.recode.x  calltime.converted.x
 Min.   :2018-03-27 17:36:51   Length:10          Min.   :1.522e+09   
 1st Qu.:2018-03-27 18:05:07   Class :character   1st Qu.:1.522e+09   
 Median :2018-03-28 16:00:51   Mode  :character   Median :1.522e+09   
 Mean   :2018-04-03 05:04:36                      Mean   :1.523e+09   
 3rd Qu.:2018-04-09 15:03:28                      3rd Qu.:1.523e+09   
 Max.   :2018-04-20 20:11:48                      Max.   :1.524e+09   

 forceTZ(full.merge.sub$calltimes.full.x[1],full.merge.sub$timezone.recode.x[1])
[1] "2018-03-27 17:36:51 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[1],full.merge.sub$timezone.recode.x[1])
[1] "2018-03-27 17:36:51 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[2],full.merge.sub$timezone.recode.x[2])
[1] "2018-04-09 15:00:22 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[3],full.merge.sub$timezone.recode.x[3])
[1] "2018-03-27 17:51:44 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[4],full.merge.sub$timezone.recode.x[4])
[1] "2018-04-20 20:11:48 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[5],full.merge.sub$timezone.recode.x[5])
[1] "2018-04-09 16:22:05 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[6],full.merge.sub$timezone.recode.x[6])
[1] "2018-03-27 18:45:16 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[7],full.merge.sub$timezone.recode.x[7])
[1] "2018-04-09 15:04:30 EDT"
> forceTZ(full.merge.sub$calltimes.full.x[8],full.merge.sub$timezone.recode.x[8])
[1] "2018-03-28 14:27:58 CDT"
> forceTZ(full.merge.sub$calltimes.full.x[9],full.merge.sub$timezone.recode.x[9])
[1] "2018-03-28 17:33:44 CDT"
> forceTZ(full.merge.sub$calltimes.full.x[10],full.merge.sub$timezone.recode.x[10])
[1] "2018-03-27 17:51:44 EDT"

我不知道为什么。

我可以尝试将新列转换为日期格式,如下所示:

as.POSIXct(full.merge.sub$calltime.converted.x,origin="1970-01-01")

但是结果仅仅是这样,而不是时区都一样,这对我没有用。

 [1] "2018-03-27 17:36:51 EDT" "2018-04-09 15:00:22 EDT" "2018-03-27 17:51:44 EDT" "2018-04-20 20:11:48 EDT" "2018-04-09 16:22:05 EDT"
 [6] "2018-03-27 18:45:16 EDT" "2018-04-09 15:04:30 EDT" "2018-03-28 15:27:58 EDT" "2018-03-28 18:33:44 EDT" "2018-03-27 17:51:44 EDT"

注意:已解决(有点)。如果在开始循环之前我将变量创建为POSIXct类变量,则它可以工作。但是,这似乎是一种奇怪的行为。

0 个答案:

没有答案