2017-01-27 6 views
1

他の時系列データをマージするために使用する時系列データフレームを作成します。POSIX DateTimeでタイムゾーンを維持する方法と、Rで強制的に導入されるNAsを回避する方法は?

私は上記のコードを実行する場合は、 'dates2010' と 'dates2011は' GMT形式である
dates2010 <- seq(as.POSIXct("2010-06-15 00:00:00", tz = "GMT"), as.POSIXct("2010-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2010 
dates2011 <- seq(as.POSIXct("2011-06-15 00:00:00", tz = "GMT"), as.POSIXct("2011-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2011 
dates <- c(dates2010, dates2011)       # combine the dates from both years 
sites <- c("a", "b", "c")         # make string of all sites 
datereps <- rep(dates, length(sites))      # repeat the date string the same number of times as there are sites 
sitereps <- rep(sites, each = length(dates))    # repeat each site in the string the same number of times as there are dates 
n <- data.frame(DateTime = datereps, SiteName = sitereps) # merge two strings with all dates and all sites 
n <- n[order(n$SiteName, n$Date),]       # re-order based on site, then date 

dates2010[1] "2011-06-15 00:00:00 GMT"。 しかし、私は、オブジェクトを作成するESTに何らかの理由でフォーマットスイッチの「さかのぼり」:dates[1] 「2010年6月14日午後7時00分00秒EST」

多分それはPOSIXクラスとは何かを持っていますか?

class(dates2010) 
[1] "POSIXct" "POSIXt" 

私は問題を切り替え時間帯を避けるためにGMTにchange the default time zone for Rにしようとしました。これは、データフレーム「n」を注文し、他のデータフレームを「n」にマージしようとすると、NAコーションエラーが発生します。

n <- n[order(n$SiteName, n$Date),] 
Warning message: 
In xtfrm.POSIXct(x) : NAs introduced by coercion 

どのようにタイムゾーンを一定に保ち、NA強制エラーを回避するかについての考えはありますか?ありがとうございました!

+3

'DateTimeClasses':?「POSIXlt」オブジェクトの_Using 'C'は、現在のタイムゾーンに変換し、そして「POSIXct」オブジェクト上の任意の「TZONE」をドロップし、それらがすべて同じでマークされている場合でも、(属性_明らかにそれを避ける方法はないので、以下のように事実の後に属性をリセットするだけです。 – alistaire

+0

あなたのメソッドは@Rich Scrivenを使って、 'dates'タイムゾーンを「GMT」に変更しました。 'dates < - 構造体(c(dates2010、dates2011)、tzone =" GMT ")'です。ありがとう。 – notacodr

+0

ああ。その情報をありがとうございます@alistaire。 – notacodr

答えて

4

c()属性を削除します。したがって、datesを作成すると、タイムゾーンが削除され、現在のロケールに自動的にデフォルト設定されました。幸いにも、structure()を使用してそこにタイムゾーンを設定することができます。 datesがすでに作成された場合

dates <- structure(c(dates2010, dates2011), tzone = "GMT") 
head(dates) 
# [1] "2010-06-15 00:00:00 GMT" "2010-06-15 01:00:00 GMT" 
# [3] "2010-06-15 02:00:00 GMT" "2010-06-15 03:00:00 GMT" 
# [5] "2010-06-15 04:00:00 GMT" "2010-06-15 05:00:00 GMT" 

、あなたはtzone属性後で変更/追加することができます。

attr(dates, "tzone") <- "GMT" 
関連する問題