5

私は、「主題」ごとに現地時間に変換しようとしている米国中の一連のデータを持っています。各イベントに UTC タイムスタンプがあり、それらを POSIXct 形式に変換しましたが、POSIXct/POSIXlt 関数 (およびを含む) のベクトルtz = DS$Factorまたはベクトルを含めようとするたびに、次のようなエラーが表示されます。tz = as.character(DS$Factor)format()strftime()

as.POSIXlt.POSIXct(x, tz = tz) のエラー: 無効な 'tz' 値

入力tz = 'US/Eastern'するだけで問題なく動作しますが、もちろん、すべての値がそのタイム ゾーンのものであるとは限りません。

各「件名」のタイムスタンプを現地時間にするにはどうすればよいですか?

DS$Factorは 5 つの値があります: US/Arizona US/Central US/Eastern US/Mountain US/Pacific

ありがとう、速記

4

3 に答える 3

1

Actually, what I did was to loop through the timezones instead of the number of rows in the data set ... then its much, much faster. I'll post code tomorrow.

In general, that's a lesson for R: don't loop through the big data frame, loop through the (much shorter) vector of categories and apply using the which() function.

As there are only 5 time zones, the loop only takes a few seconds now.

One other caveat is that if you put it into POSIXct format it will still graph the times in your machine's local timezone. So you need an extra step to then covert it into local time using force_tz().

cap$tdiff is really just created to make sure that the code is doing what it says it should be doing.

library("lubridate")    

tzs <- as.character(unique(cap$timezone))

cap$localtimes <- as.POSIXlt(0,origin = "1970-01-01")

#now loop through by timezone instead of lines of cap[]
for (i in 1:length(tzs)) {
  whichrows <- which(cap$timezone == tzs[i])

  cap[whichrows,"localtimes"] <-
    with_tz(cap[whichrows,"UTC"],tzone = tzs[i])
}

remove(i, whichrows)

cap$tdiff <- as.numeric((force_tz(cap$localtime, "UTC") - cap$UTC))
cap$localtime <- as.POSIXct(force_tz(cap$localtimes))
于 2015-11-23T23:52:37.803 に答える
0

そのため、これを行うための for ループを作成できましたが、遅く、実行に約 10 分かかります。私は構文を理解できませんでしたapply().データストアには768kの観測があり、成長しているため、この操作を実行するためのより高速で並列化可能な方法を作成するのに役立つことは間違いありません.

>     require(lubridate)
>     
>     loct = NULL for (i in 1:nrow(DS))
>     {
>       loct[i] <- with_tz(DS$UTC[i],tzone =
>       ifelse(DS$timezone[i]=="","US/Eastern",as.character(DS$timezone[i])))
>     } DS$localtime <- as.POSIXct(loct, origin ="1970-01-01") remove (loct, i)
于 2015-08-21T14:31:49.437 に答える