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.
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")
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")
This question already has answers here:
Formatting a date in R without leading zeros
(4 answers)
Closed 2 years ago.
I have dates in a character vector and I'm looking for a way to transform it to date format with as.Date():
vector <- c("15/1/2019", "5/5/2019")
However, as.Date(vector, format="%d/%m/%Y") receives as input characters with zeros leading. And I have found plenty of solutions for:
Input: "15/01/2019"
Output: "15/1/2019"
However, I need exactly the opposite to make as.Date() to work:
Input: "15/1/2019" "5/5/2019"
Output: "15/01/2019" "05/05/2019"
Is there any specific formatting for format argument? Or I have to use a substr() solution?
We can use strftime.
strftime(as.Date(inp, format="%d/%m/%Y"), format="%d/%m/%Y")
# [1] "15/01/2019" "05/05/2019"
Data
inp <- c("15/1/2019", "5/5/2019")
I'm not sure I understand the issue. as.Date() works as expected on your input:
vector <- c("15/1/2019", "5/5/2019")
dates <- as.Date(vector, format = "%d/%m/%Y")
dates
[1] "2019-01-15" "2019-05-05"
To get the desired output, you use format:
format(dates, "%d/%m/%Y")
[1] "15/01/2019" "05/05/2019"
EDIT
From your comments, it's clear that you are confusing two different things.
To get a Date object using as.Date(), you need to tell it the format of the input date character string. You have "day-month-year". So you supply that to as.Date():
as.Date(vector, format = "%d/%m/%Y")
The result will always be displayed in the console as year-month-day (YYYY-MM-DD).
Displaying the date in a chosen format is something different. Now we are formatting the Date object back to a character string:
format(dates, "%d/%m/%Y")
To summarise: a properly-converted Date object will always display in the console as YYYY-MM-DD. Formatting that object as a string is a different operation.
I hope this clears things up.
I read a .xlsx file containing Date columns into R and converted it into dataframe.
Some date columns are being read correctly but most of the others are getting converted to "43116" format.
Any attempt to convert it into Date using as.Date(, origin= <>, format=<>) is returning NA.
I have tried all possible solutions like using 'stringAsFactors = FALSE', POSIT thing and checking the excel file for date formats but nothing worked.
Please help.
It is difficult to recreate the problem if no data is provided, but if you want to convert the number 43129 or the character "43129" to a R date you should do the following:
a <- 43129
b <- '43129'
format(as.Date(a, origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
format(as.Date(as.integer(b), origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
I used the format yyyy-mm-dd, but any other date format could be used if you format it properly.
Hope it helps!
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"