Following is the error I am getting when trying to convert the date column read from csv
data<-read.csv("data.csv",stringsasfactors=FALSE) #data<-read.table("data.csv",sep=",",header=TRUE)
as.Date(data$Date,"%Y-%d-%m")
Error:
error in as.date.default(x ...) do not know how to convert 'x' to class date
If your dates look like 30-06-2003, then you need to adjust the format in which you want to read them accordingly. With this, I mean that the right format is "%d-%m-%Y", i.e. day - month - 4 digit year, and not "%Y-%d-%m".
Try this line
as.Date(data$Date, format = "%d-%m-%Y")
Related
I have an excel spreadsheet that has different date formats. R reads in the dates correctly when they're formatted as m/dd/yy, but when they're m/d/yy, it reads them as a code like this: 36251.
To fix it, I found this, which seems useful, but it is only for one date and I have multiple.
How to convert Excel date format to proper date in R
This was what was suggested:
as.Date(42705, origin = "1899-12-30")
So I tried this:
stockindices0$Date <- as.Date(stockindices0$Date , origin = "12-30-99")
but got the error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I also tried it with the year as 1899 with no success.
You can try this
as.Date(stockindices0$Date, format = "%d/%m/%y")
I have a .csv file with several dates presented like this: "26-11-21". These dates are in character format and when I try to convert them to date format using the following code, they are converted to Unknown format and appear as "2021-11-26":
file$Date1 <- as.Date(as.character(file$Date1),format="%d-%m-%y")
I have also tried the two following codes but it does not work either:
file$Date1 <- as.Date(paste0(as.character(file$Date1)), format = "%d-%m-%y")
file$Date1 <- format(as.Date(file$Date1, "%d-%m-%y"), "%d-%m-%y")
Thank you for your help.
I have this character type vector that consists of a year and a month. I want to convert it to a date type, but when I try to do this with the POSIXct function, I get the error:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
I can't seem to figure out why it won't work. Anyone?
old <- as.character("201702")
library(lubridate)
new <- as.POSIXct(date, origin = "201501")
You just need to convert to a date first with the appropriate formatting:
as.POSIXct(as.Date("201702", format = "%Y%m"))
You could also assume the you want the 1st of the month?
as.POSIXct(as.Date(paste0("201702", "01"), format = "%Y%m%d"))
Be careful with POSIX dates because they contain the time as well and then timezone that you specify can therefore also play a role in the date you get.
I have a dataset called EPL2011_12. I would like to make new a dataset by subsetting the original by date. The dates are in the column named Date The dates are in DD-MM-YY format.
I have tried
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > 13-01-12)
and
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > "13-01-12")
but get this error message each time.
Warning message:
In Ops.factor(Date, 13- 1 - 12) : > not meaningful for factors
I guess that means R is treating like text instead of a number and that why it won't work?
Well, it's clearly not a number since it has dashes in it. The error message and the two comments tell you that it is a factor but the commentators are apparently waiting and letting the message sink in. Dirk is suggesting that you do this:
EPL2011_12$Date2 <- as.Date( as.character(EPL2011_12$Date), "%d-%m-%y")
After that you can do this:
EPL2011_12FirstHalf <- subset(EPL2011_12, Date2 > as.Date("2012-01-13") )
R date functions assume the format is either "YYYY-MM-DD" or "YYYY/MM/DD". You do need to compare like classes: date to date, or character to character. And if you were comparing character-to-character, then it's only going to be successful if the dates are in the YYYYMMDD format (with identical delimiters if any delimiters are used).
The first thing you should do with date variables is confirm that R reads it as a Date. To do this, for the variable (i.e. vector/column) called Date, in the data frame called EPL2011_12, input
class(EPL2011_12$Date)
The output should read [1] "Date". If it doesn't, you should format it as a date by inputting
EPL2011_12$Date <- as.Date(EPL2011_12$Date, "%d-%m-%y")
Note that the hyphens in the date format ("%d-%m-%y") above can also be slashes ("%d/%m/%y"). Confirm that R sees it as a Date. If it doesn't, try a different formatting command
EPL2011_12$Date <- format(EPL2011_12$Date, format="%d/%m/%y")
Once you have it in Date format, you can use the subset command, or you can use brackets
WhateverYouWant <- EPL2011_12[EPL2011_12$Date > as.Date("2014-12-15"),]
I wish to import my csv file into a data frame but the date in my csv file is in a non-standard format.
The date in the first column is in the following format:
08.09.2016
One of the arguments in my read.csv2 functions is to specify the classes and when I specify this column as a date I receive the following error upon execution:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm guessing it doesn't like converting the date from factor class to date class.
I've read a little about POSIXlt but I don't understand the details of the function.
Any ideas how to convert the class from factor to date??
When you convert character to date, you need specify format if it is not standard. The error you got is the result of as.Date("08.09.2016"). But if you do as.Date("08.09.2016", format = "%m.%d.%Y"), it is fine.
I am not sure whether it is possible to pass format to read.csv2 for correct date formatting (maybe not). I would simply read in this date column as factor, then do as.Date(as.character(), format = "%m.%d.%Y") on this column myself.
Generally we use the following format "dd/mm/yy" how can I reorganise the date to that format?
Use format(, format = "%d/%m/%y").
A complete example:
format(as.Date("08.09.2016", format = "%m.%d.%Y"), format = "%d/%m/%y")
# [1] "09/08/16"