3

データセットで「date」(class=POSIXlt の変数) の時間をローカライズしようとすると、エラーが発生します。コード例は次のとおりです。

# All dates are coded by survey software in EST(not local time)
date <- c("2011-07-26 07:23", "2011-07-29 07:34", "2011-07-29 07:40")
region <-c("USA-EST", "UK", "Singapore")

#Change the times based on time-zone differences
start_time<-strptime(date,"%Y-%m-%d %h:%m")
localtime=as.POSIXlt(start_time)
localtime<-ifelse(region=="UK",start_time+6,start_time)
localtime<-ifelse(region=="Singapore",start_time+12,start_time)

#Then, I need to extract the hour and weekday
weekday<-weekdays(localtime)
hour<-factor(localtime)

"ifelse"エラーが発生するため、ステートメントに何か問題があるに違いありません: number of items to replace is not a multiple of replacement length。助けてください!

4

2 に答える 2

2

R のネイティブ タイム コードを使用するのはどうでしょうか。秘訣は、POSIX ベクトルに複数のタイムゾーンを含めることはできないため、代わりにリストを使用することです。

region <- c("EST","Europe/London","Asia/Singapore")
(localtime <- lapply(seq(date),function(x) as.POSIXlt(date[x],tz=region[x])))
[[1]]
[1] "2011-07-26 07:23:00 EST"

[[2]]
[1] "2011-07-29 07:34:00 Europe/London"

[[3]]
[1] "2011-07-29 07:40:00 Asia/Singapore"

そして、単一のタイムゾーンでベクトルに変換するには:

Reduce("c",localtime)
[1] "2011-07-26 13:23:00 BST" "2011-07-29 07:34:00 BST"
[3] "2011-07-29 00:40:00 BST"

私のシステムのタイムゾーンは BST ですが、EST の場合はそれに変換されます。

于 2012-07-24T17:05:23.683 に答える
0

に組み込まれているタイムゾーン処理を使用できますPOSIXct

> start_time <- as.POSIXct(date,"%Y-%m-%d %H:%M", tz = "America/New_York")
> start_time
[1] "2011-07-26 07:23:00 EDT" "2011-07-29 07:34:00 EDT" "2011-07-29 07:40:00 EDT"
> format(start_time, tz="Europe/London", usetz=TRUE)
[1] "2011-07-26 12:23:00 BST" "2011-07-29 12:34:00 BST" "2011-07-29 12:40:00 BST"
> format(start_time, tz="Asia/Singapore", usetz=TRUE)
[1] "2011-07-26 19:23:00 SGT" "2011-07-29 19:34:00 SGT" "2011-07-29 19:40:00 SGT"
于 2012-07-24T17:04:53.200 に答える