$ 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"
Related
I have a csv file with dates as characters in the following format: 202211, 202210, 202209 etc.
I tried using
xdate<- as.Date("202211", format="%Y%m")
xdate
but the output I get is NA
This works if the format would be 20221111
xdate<- as.Date("20221111", format="%Y%m%d")
xdate
[1] "2022-11-11"
Is there a way to solve this problem without adding a day to the dates?
If your date is always the first day of month, then you can paste "01" at the end of the string and then apply as.Date
xdate<- as.Date(paste0("202211", "01"), format="%Y%m%d")
xdate
[1] "2022-11-01"
Using lubridate avoids manually adding a day to the dates:
library(lubridate)
ym("202211")
[1] "2022-11-01"
I imported csv data saved from excel and I am using a Mac if that matters.
To simplify things, I have been working with one particular entry from my data "Jan-12". This is of type character and in the form Month-Year.
This is what I have tried:
as.Date("Jan-12", format = "%b-%y")
I keep getting NA. I have browsed through other answers but haven't been able to figure out whats happening.
as.Date() help page says:
If the date string does not specify the date completely, the returned
answer may be system-specific.
If you really need to use as.Date() you can append some fixed day (say 1st) to date and convert
mydate <- "Jan-12"
as.Date(paste0("01-", mydate), format= "%d-%b-%y")
"2012-01-01"
Or you can use lubridate::fast_strptime():
library(lubridate)
fast_strptime("Jan-12", "%b-%y")
"2012-01-01 UTC"
Here is another option using zoo, then you can use as.Date.
library(zoo)
as.Date(as.yearmon("Jan-12", "%b-%y"))
# [1] "2012-01-01"
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
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.
This should be simple but I can't figure it out. How should I go about formatting dates that are '20150703' into '07-03-2015'? Thanks
You may use format after converting to 'Date' class
format(as.Date(dates, '%Y%m%d'), '%m-%d-%Y')
#[1] "07-03-2015"
data
dates <- '20150703'
Also take a look at lubridate package here which makes it easier to work with dates.
ymd("20150703")
gives
[1] "2015-07-03 UTC"