0

I have a dataframe (data_frame) with two columns.

> head(data_frame)
                  Date Rainfall
1 1992-01-06 14:00:00      0.3
2 1992-01-06 15:00:00      0.2
3 1992-01-06 16:00:00      0.3
4 1992-01-06 18:00:00      0.1
5 1992-01-06 19:00:00      0.3
6 1992-01-06 20:00:00      0.8

Date is of class POSIXct. Rainfall is numeric.

> class(data_frame$Date)
[1] "POSIXct"

When I try to create a time series using xts, I get this error:

> time_series = xts(data_frame$Rainfall, order.by = data_frame$Date)

Error in xts(data_frame$Rainfall, order.by = data_frame$Date) :
order.by requires an appropriate time-based object

xts should be able to handle POSIXct. I went through a similar question posted here, where the solution was to convert date into the above format. Looking at those answers, my code should work. I can't figure out why it is not.

reproducible example:

head_data_frame = structure(list(
  Date = structure(
    c(
      694659600,
      694663200,
      694666800,
      694674000,
      694677600,
      694681200
    ),
    class = "POSIXct"
  ),
  Rainfall = c(0.3,
               0.2, 0.3, 0.1, 0.3, 0.8)
),
row.names = c(NA, 6L),
class = "data.frame")

0 Answers0