3

私はこの時系列データを持っています:

     "timestamp"          "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999

次のコードを使用しています。

d <- read.table(Name[1], header=TRUE)  #Name[1] is text file containing data

d <- read.zoo(d,
 format="'%Y-%m-%d %H:%M:%S'", 
 FUN=as.POSIXct  )

このエラーが表示されます:

Error in read.zoo(d, format = "'%Y-%m-%d %H:%M:%S'", FUN = as.POSIXct) : 
 index has 5 bad entries at data rows: 1 2 3 4 5

この問題について助けてもらいたいです。ご検討をお願いいたします。

4

2 に答える 2

4

-05これは、各日付/時刻の最後に無視することを許可した投稿のデータで機能します。(ファイルから読み取るには、コメントアウトされた行のようなものを使用します。)

Lines <- '"timestamp"          "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999'

library(zoo)
# z <- read.zoo("myfile.txt", tz = "")
z <- read.zoo(text = Lines, tz = "")

上記のコードからの出力は次のとおりです。

> z
                    depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom
2012-05-23 17:30:08 16.37                       17.16                               0.79
2012-05-23 17:45:13 16.35                       17.16                               0.81
2012-05-23 18:00:03 16.39                       17.16                               0.77
2012-05-23 18:15:08 16.38                       17.16                               0.78
2012-05-23 18:30:12 16.40                       17.16                               0.76

詳細については、 ?read.zoo?read.table、およびvignette ("zoo-read")を試してください。read.zoo最後の 1 つは、例を示すことに焦点を当てたドキュメント全体です。

編集: 解説へのリンクを追加しました。

于 2012-05-28T10:57:36.893 に答える
4

タイムスタンプ データに不適切な形式のタイムゾーン データ、つまり-05各タイムスタンプの末尾が含まれています。

From ?strptimeI learn that you can %zformat to format the signed timezone offset. 符号付きの 4 桁の数字にする必要があります-0500

%z
Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC.

したがって、00タイムスタンプに欠落を追加する回避策は次のとおりです。

データを再作成します。

dat <- '
"timestamp" "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom"
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999
'

不足しているゼロを追加します。

x <- read.table(text=dat, header=TRUE)
x$timestamp <- paste(x$timestamp, "00", sep="")
x$timestamp <- as.POSIXct(x$timestamp, format="%Y-%m-%d %H:%M:%S%z")
x

動物園に変身

library(zoo)
as.zoo(x)
  timestamp           depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom
1 2012-05-24 00:30:12 16.40 17.16                       0.76                              
2 2012-05-24 00:15:08 16.38 17.16                       0.78                              
3 2012-05-24 00:00:03 16.39 17.16                       0.77                              
4 2012-05-23 23:45:13 16.35 17.16                       0.81                              
5 2012-05-23 23:30:08 16.37 17.16                       0.79   
于 2012-05-28T07:40:30.297 に答える