Convert data into xts in R with date format YYYYMMDD - r

I have a time series table named ff5 imported into R using read.csv with the date column in format of "YYYYMMDD".
I installed the xts package to better handle time series data. I tried to use the below code to convert the original data ff5 to xts format:
library(xts)
ff5_xts <- xts(ff5, order.by = as.Date(ff5["date"], "%Y%m%d"))
And I got this error message:
Error in as.Date.default(x, ...) : do not know how to convert 'x'
to class “Date”
I tried a few other ways with or without xts but could not figure out how to convert this original data into time series.
Would appreciate any help!

Does this work?
ff5 <- data.frame(date=c("20180615", "20180617", "20180616"))
ff5$date <- as.Date(ff5$date, "%Y%m%d")
library(xts)
ff5_xts <- xts(ff5, order.by = ff5$date)
ff5_xts
date
2018-06-15 "2018-06-15"
2018-06-16 "2018-06-16"
2018-06-17 "2018-06-17"

Related

Converting timestamps to POSIXct format returns double

I want to convert a data frame to a ltraj list using the R package adehabitatLT, and for doing so I must convert timestamps to POSIXct format. I am writing the following code:
martin_df$timestamp <- as.POSIXct(martin_df$timestamp,
format="%Y-%m-%d %H:%M:%OS", tz="GTM")
and throws no error. But then when I ask for:
typeof(martin_df$timestamp)
it tells me is double.
martin.ltraj <- as.ltraj(martin_df, typeII = TRUE)
Error in as.ltraj(martin_df, typeII = TRUE) : For objects of type II,
date should be of class "POSIXct"
Does anyone know where the error is?

Problems with parse_date_time converting a character vector

I have an imported CSV in R which contains a column of dates and times - this is imported into R as character. The format is "30/03/2020 08:59". I want to convert these strings into a format that allows me to work on them. For simplicity I have made a dataframe which has a single column of these dates (854) in this format.
I'm trying to use the parse_date_time function from lubridate.
It works fine when I reference a single value, e.g.
b=parse_date_time(consults_dates[3,1],orders="dmy HM")
gives b=2020-03-30 09:08:00
However, when I try to perform this on the entire(consults_dates), I get an error, e.g.
c= parse_date_time(consults_dates,orders="dmy HM") gives error:
Warning message:
All formats failed to parse. No formats found.
Apologies - if this is blatantly a simple question, day 1 of R after years of Matlab.
You need to pass the column to parse_date_time function and not the entire dataframe.
library(lubridate)
consults_dates$colum_name <- parse_date_time(consults_dates$colum_name, "dmy HM")
However, if you have only one format in the column you can use dmy_hm
consults_dates$colum_name <- dmy_hm(consults_dates$colum_name)
In base R, we can use :
consults_dates$colum_name <- as.POSIXct(consults_dates$colum_name,
format = "%d/%m/%Y %H:%M", tz = "UTC")

Failed to convert 'x$date' to class “Date” in R

Okay so I have been trying to use this package from Facebook, but for some reason I keep seeing this error.
library(tidyquant)
library(quantmod)
library(prophet)
library(dplyr)
SPY <-tq_get(get = "stock.prices", "SPY", from = "2016-01-01")
df<-select(SPY,c(date,close))
df$date <- as.Date(as.character(df$date),format="%Y-%m-%d")
colnames(df)<-c("ds","y")
m<-prophet(df)
future<-make_future_dataframe(m,periods=52, freq = "d")
forecast <- predict(m,future)
plot(m,forecast)
When I run the plot function, I would see this error message:
Error in as.Date.default(x$date, format = "%d/%m/%Y") : do not know how to convert 'x$date' to class “Date”
I tried using as.Date function, strptime function, and format function but it was in no use.
forecast$ds<-as.Date(paste(forecast$ds),"%Y-%m-%d")
forecast$ds<- format(forecast$ds, "%d/%m/%Y")
forecast$date<-forecast$ds
m$date<-forecast$ds
This didn't work
df$newdate<- strptime(as.character(df$ds),"%Y-%m-%d")
df$newdate<- format(df$newdate, "%d/%m/%Y")
df$newdate<-as.Date(df$newdate)
dp<-data.frame(df$newdate,y)
and this didn't work either. They were some answers provided by other similar postings but I do not really see what is causing the issue. Any help would be appreciated.
The error message is caused by some quirks of as.Date(). The workaround is to save the dataset as a CSV file using write.csv() and then read in again as a CSV using read.csv(). And then use as.Date(). This will eliminate the error message.
Another workaround is to use as.data.frame() first for your entire dataset before using as.Date().
library(lubridate)
df$date <- ymd(df$date) # ymd stands for year, month, date
or
library(anydate)
df$date <- anydate(df$date)
Plotting works afterwards for me.

Smartbind date format and error in R

I'm getting an error using smartbind to append two datasets. First, I'm pretty sure the error I'm getting:
> Error in as.vector(x, mode) : invalid 'mode' argument
is coming from the date variable in both datasets. The date variable in it's raw format is such: month/day/year. I transformed the variable after importing the data using as.Date and format
> rs.month$xdeeddt <- as.Date(rs.month$xdeeddt, "%m/%d/%Y")
> rs.month$deed.year <- as.numeric(format(rs.month$xdeeddt, format = "%Y"))
> rs.month$deed.day <- as.numeric(format(rs.month$xdeeddt, format = "%d"))
> rs.month$deed.month <- as.numeric(format(rs.month$xdeeddt, format = "%m"))
The resulting date variable is as such:
> [1] "2014-03-01" "2014-03-13" "2014-01-09" "2013-10-09"
The transformation for the date was applied to both datasets (the format of the raw data was identical for both datasets). When I try to use smartbind, from the gtools package, to append the two datasets it returns with the error above. I removed the date, month, day, and year variables from both datasets and was able to append the datasets successfully with smartbind.
Any suggestions on how I can append the datasets with the date variables.....?
I came here after googling for the same error message during a smartbind of two data frames. The discussion above, while not so conclusive about a solution, definitely helped me move through this error.
Both my data frames contain POSIXct date objects. Those are just a numeric vector of UNIXy seconds-since-epoch, along with a couple of attributes that provide the structure needed to interpret the vector as a date object. The solution is simply to strip the attributes from that variable, perform the smartbind, and then restore the attributes:
these.atts <- attributes(df1$date)
attributes(df1$date) <- NULL
attributes(df2$date) <- NULL
df1 <- smartbind(df1,df2)
attributes(df1$date) <- these.atts
I hope this helps someone, sometime.
-Andy

Converting zoo to ts before forecasting

I am struggling to convert a zoo objects to a ts object.
I have a huge data.frame "test" with quarterly hour data, which looks like this:
date <- c("2010-07-04 09:45:00", "2010-07-04 10:00:00", "2010-07-04 10:15:00", "2010-07-04 10:30:00", "2010-07-04 10:45:00", "2010-07-04 11:00:00")
nrv <- c("-147.241", "-609.778", "-432.289", "-340.418", "-73.96" , "-533.108")
tt <- c("3510.7", "3608.5", "3835.7", "4003.7", "4018.8", "4411.9")
test <- data.frame(date,nrv,tt)
test
I want to make some predictions (mostly ARIMA) and thought the forecastpackage would be a good idea for that.
First of I formated the data away from characters.
test$date <- strptime(test$date,format="%Y-%m-%d %H:%M")
test$nrv <- as.numeric(as.character(test$nrv))
test$tt <- as.numeric(as.character(test$tt))
str(test) #date is POSIXlt object
Since I needed to do an interpolation and construct lags, I also used the zoo package using the date variable as index, which worked great. The `zoo package was recommended to me while dealing with time series data.
library(zoo)
test.zoo <- zoo(test[,2:3],test[,1])
test.zoo #date is now the Index and and the zoo objects works nicely
But then I realized that forecasting only seems to work with ts objects. (Is that true?)
When I tried to convert the zoo object to a ts object, my time index disappeared. I think this might be due to not using a proper frequency. However I am somewhat lost as to what would be a working frequency for this dataset and with ts objects in general.
test.ts <- as.ts(test.zoo)
test.ts
How do I convert this zoo object back to a ts object I can use for forecasting?
Thanks!
The forecast package only works with ts objects as you suspected.
You can use test.ts with the forecast package. For example
plot(forecast(test.ts[,1]))
I had the same problem and solved it by using zooreg function.
step1: use zooreg to transform zoo object to non-zoo but ts alike objet
step2: use ts function to transform further to ts object

Resources