I wrote the following script in R software in order to converting date format in a prospective cohort data. But, I get an odd error. Could everyone help me? Any idea?
Code:
for(col in colnames(outcome)){
if(startsWith(col, "eventdate_")){
outcome[, col] <- as.Date(outcome[, col],"%Y-%m-%d")
}
}
Error:
Error in as.Date.default(outcome[, col], format = "%Y-%m-%d") :
do not know how to convert 'outcome[, col]' to class “Date”
Related
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?
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"
I am trying to convert the odd columns of a dataframe to a POSIXct format.
My problem is the following one. If I run this:
as.POSIXct(timestamptest2[2,1])
I got the desired format:
"2018-05-01 15:00:16 CEST"
However when I am doing the conversion to all the columns I get this error:
as.POSIXct(timestamptest2[,odd_indexes])
Error in as.POSIXct.default(timestamptest2[, odd_indexes]) :
do not know how to convert 'timestamptest2[, odd_indexes]' to class “POSIXct”
Being odd_indexes a vector containing the colums date have my date in string format.
I have tried as well with:
-apply(timestamptest2[,odd_indexes],2,as.POSIXct)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
Anyone knows how to deal with this problem?
This should work:
timestamptest2[odd_indexes] <- lapply(timestamptest2[odd_indexes], as.POSIXct)
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.
I'm trying to convert my date column from character to date format, which I thought should be dead easy using:
datetest <- as.Date(CAT$Date, "%Y-%m-%d")
but it returns:
Error in as.Date.default(CAT$Date, "%Y-%m-%d") : do not know how to
convert 'CAT$Date' to class “Date”
I have also tried: datetest <- as.Date(CAT[["Date"]], "%Y-%m-$d")
but get the same error message.
Really not sure why it doesn't like it, any help to a complete newbie would be appreciated! Thanks.
first check the class of CAT$Date, then do the following:
datetest <- as.Date(as.character(unlist(CAT$Date)), "%Y-%m-%d")