I have a sample date column as part of large data set, Below date is in multiple format.
I need convert below mentioned into Date format , Please help me with a solution.
22-04-2015
4/8/2015
18-04-2015
5/7/2015
26-05-2015
6/12/2015
24-06-2015
23-06-2015
Try with lubridate. The function guess_formats() allows defining possible formats of your data (you could add others if needed), and then you can use as.Date() to get the dates in the proper class using the formats previously defined. Here the code:
library(lubridate)
#Dates
vecdate <- c('22-04-2015', '4/8/2015','18-04-2015','5/7/2015','26-05-2015',
'6/12/2015','24-06-2015','23-06-2015')
#Formats
formats <- guess_formats(vecdate, c("dmY"))
dates <- as.Date(vecdate, format=formats)
Output:
dates
[1] "2015-04-22" "2015-08-04" "2015-04-18" "2015-07-05" "2015-05-26" "2015-12-06" "2015-06-24"
[8] "2015-06-23" "2015-04-22" "2015-08-04" "2015-04-18" "2015-07-05" "2015-05-26" "2015-12-06"
[15] "2015-06-24" "2015-06-23"
Have you tried to use R package anytime for date conversion?
> library(anytime)
> datemix <- c("22-04-2015", "4/8/2015", "18-04-2015", "5/7/2015")
> anydate(datemix)
[1] NA "2015-04-08" NA "2015-05-07"
You notice that the first and third character dates came up with NA. This is because the package anytime as currently defined does not include this format "d-m-y". It is very easy to add this format with the addFormats() command as shown below
> addFormats("%d-%m-%Y")
> datemix <- c("22-04-2015", "4/8/2015", "18-04-2015", "5/7/2015")
> anydate(datemix)
[1] "2015-04-22" "2015-04-08" "2015-04-18" "2015-05-07"
The output dates are converted into ISO format of YYYY-MM-DD.
You can explore all the formats in anytime using getFormats() command. Here is the link to the anytime on R CRAN https://cran.r-project.org/web/packages/anytime/anytime.pdf
Related
A client sent me an Excel file with dates formatted as e.g 3/15/2012 for March 15. I saved this as a .csv file and then used
camm$Date <- as.Date(camm$Date, "%m/%d/%y")
but this gave me values starting in the year 2020!
I tried to reformat the dates in the original csv file so that they were e.g. 03/14/2013 but was unable to do so.
Any help appreciated
Use capital Y in as.Date call instead. This should do the trick:
> as.Date("3/15/2012", "%m/%d/%Y")
[1] "2012-03-15"
From the help file's examples you can realize when year is full specified you should use %Y otherwise %y for example:
> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
> as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
You can see that in your example the Year format is 2012 then you should use %Y, and in the other example (taken from the as.Date help file) Year format is 92 then using %y is the correct way to go. See as.Date for further details.
You might also give a try to the lubridate package if you do not want to deal with the hieroglyphics :)
> library(lubridate)
> parse_date_time('3/15/2012', 'mdy')
1 parsed with %m/%d/%Y
[1] "2012-03-15 UTC"
PS.: of course I do not encourage anyone to use any extra dependencies, this answer was just posted here as an alternative (and quick to remeber) solution
To complete the picture, you might also try the recently introduced (2016-09) package anytime which takes advantage of the Boost C++ libraries:
anytime::anytime("3/15/2012")
#[1] "2012-03-15 CET"
We can use mdy from lubridate
lubridate::mdy('3/15/2012')
#[1] "2012-03-15"
Or parse_date from readr which uses same format as as.Date
readr::parse_date('3/15/2012', '%m/%d/%Y')
#[1] "2012-03-15"
I have imported a sas file with date ion the format 21JAN1988:00:00:00 seen as factor in R.
I want to convert this into an r date format 1988-01-21.
How do I go about this?
Using as.Date:
x <- "21JAN1988:00:00:00"
as.Date(x, format = "%d%b%Y")
# [1] "1988-01-21"
Using anytime package:
anytime::anydate(x)
# [1] "1988-01-21"
anytime::anytime(x)
# [1] "1988-01-21 01:00:00 GMT"
Note: There must be a duplicate for this post, but I couldn't find. Let me know in the comments if you find one, I will convert this post to community wiki.
$ date "20141013T000000", "20141209T000000", "20150225T000000", "20141209T000000"
I have this "date" variable in R dataframe. I want to clean this variable and remove "T000000" such that "20141013T000000" shows only "20141013" so I can then convert my date variable into it's proper date format.
Thank you very much.
You don't need to remove that, you can just do the date-conversion directly and specify the existing format:
as.Date("20141013T000000", "%Y%m%dT000000")
# [1] "2014-10-13"
Or a lubridate solution:
data <- "20141013T000000"
library(lubridate)
as_datetime(date)
#[1] "2014-10-13 UTC"
I'm trying to use as.Date in R.
I'm using the command:
as.Date("65-05-14", "%y-%m-%d")
I get:
"2065-05-14"
Is there any way to get it show 1965 instead? Or do I need to recode everything into long format -- eg add 1900 as a numeric?
Thanks!
I didn't see this simple solution in the linked questions, so I'm adding it here too.
In base R you can simply use as.POSIXlt class which provides year attribute. You can then simply reduce 100 years.
Lets say this is your dates vector
(Date <- c("65-05-14", "15-05-14", "25-05-14", "34-05-14"))
## [1] "65-05-14" "15-05-14" "25-05-14" "34-05-14"
You can simply do
Date <- as.POSIXlt(Date, format = "%y-%m-%d")
Date$year <- Date$year - 100L
Date # Alternatively, you could also do `as.Date(Date)`
## [1] "1965-05-14 IDT" "1915-05-14 IDT" "1925-05-14 IDT" "1934-05-14 IDT"
A client sent me an Excel file with dates formatted as e.g 3/15/2012 for March 15. I saved this as a .csv file and then used
camm$Date <- as.Date(camm$Date, "%m/%d/%y")
but this gave me values starting in the year 2020!
I tried to reformat the dates in the original csv file so that they were e.g. 03/14/2013 but was unable to do so.
Any help appreciated
Use capital Y in as.Date call instead. This should do the trick:
> as.Date("3/15/2012", "%m/%d/%Y")
[1] "2012-03-15"
From the help file's examples you can realize when year is full specified you should use %Y otherwise %y for example:
> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
> as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
You can see that in your example the Year format is 2012 then you should use %Y, and in the other example (taken from the as.Date help file) Year format is 92 then using %y is the correct way to go. See as.Date for further details.
You might also give a try to the lubridate package if you do not want to deal with the hieroglyphics :)
> library(lubridate)
> parse_date_time('3/15/2012', 'mdy')
1 parsed with %m/%d/%Y
[1] "2012-03-15 UTC"
PS.: of course I do not encourage anyone to use any extra dependencies, this answer was just posted here as an alternative (and quick to remeber) solution
To complete the picture, you might also try the recently introduced (2016-09) package anytime which takes advantage of the Boost C++ libraries:
anytime::anytime("3/15/2012")
#[1] "2012-03-15 CET"
We can use mdy from lubridate
lubridate::mdy('3/15/2012')
#[1] "2012-03-15"
Or parse_date from readr which uses same format as as.Date
readr::parse_date('3/15/2012', '%m/%d/%Y')
#[1] "2012-03-15"