how to read time series in xts?
r, time-series, xts
Solution
This works on the data in the post provided its OK to ignore the `-05` at the end of each date/time. (To read from a file use something like the commented out line.)
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 = "")
The output from the above code is:
> 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
For more info try ?read.zoo and ?read.table and also vignette("zoo-read"). The last one is an entire document focused on giving `read.zoo` examples.
EDIT: Added links to commentary.
Problem
I have this time series data: ``` "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 ``` I am using the following code: ``` 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 ) ``` Its giving me this error: ``` 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 ``` I wish to get help on this problem. Thank you for your consideration.