How to convert date format in R - r

My data contains a variable which recorded the date like "Wednesday 01/01/86". I want to drop the days like "Wednesday" and convert "01/01/86" into the format "1986-01-01".
I tried as.Date(myData$DATE) but I got an error message: character string is not in a standard unambiguous format. I assume this is due to the character Wednesday?

You have to tell as.Date what format your input is in:
as.Date("Wednesday 01/01/86", format="%A %m/%d/%y")
# "1986-01-01"
See docs for strptime for more details.

Related

From character to posixct

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.

read.csv2 date formatting in R

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"

Converting dates with "unconventional" formats

I am not too great with manipulating dates in R, and am currently stuck on trying to convert a date that has an ambiguous format into a Date object. My dates are in the format 20150122, for example. When I use as.Date, I get an error like:
d7$Search.Date <- as.Date(d7$Search.Date, "%y%m%d")
Error in charToDate(x) :
character string is not in a standard unambiguous format
How can I convert these currently? Thank you.
We need to use %Y as the year part is 4 digits, %y is used when it is 2 digit.
as.Date(d7$Search.Date, "%Y%m%d")
For example
as.Date("20150122", "%Y%m%d")
#[1] "2015-01-22"
For more info regarding the format, check ?strptime

Unable to convert this Character type to a proper Date type in R

I have a data set that has a column for dates (emp2$hiredate) which is in char format. While converting it to Date, I'm getting the following error :
> date1 <- emp2$hiredate[2]
> str(date1)
chr "20FEB1981"
> as.POSIXct(date1)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
How do I solve this problem?
R doesn't automatically know the format of the date in the string. You need to use the format argument to tell it how to process the string.
You can use as.Date to create a date
as.Date("20FEB1981", "%d%b%Y")
# [1] "1981-02-20"
Or use as.POSIXct with the format option to create a date-time
as.POSIXct("20FEB1981", format = "%d%b%Y")
# [1] "1981-02-20 PST"
%d means the day of the month in decimal form
%b means that the month is not in numeric form, but rather an abbreviated name
%Y (capitalized) means the year is in century form (all four numbers)

convert from posix timestamp in %Y-%m-%d in R

i have date in this format:
1397758632
and i need to convert them into dates in this format %Y-%m-%d.
how can i do this? my timezone is GTM.
Using as.POSIXct() function in this way:
as.POSIXct(x="1397758632",origin="1960-01-01", tz="GMT")
i have this error
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
how can i obtain a readable data?
A couple of things, if you have defined a variable x = "1397758632" you don't have to declare it again, just use x. eg// as.POSIXct(x,origin="1970-01-01", tz="GMT")
2nd, x="1397758632" is actually a char variable, try str(x) you would want to declare it as numeric, x=1397758632 and you should be all good.
Lastly, add as.Date() if you just want the date and not the whole time stamp.
So something like:
x=1397758632
as.POSIXct(x,origin="1970-01-01", tz="GMT")
as.Date(as.POSIXct(x,origin="1970-01-01", tz="GMT")) # for just the date

Resources