Date in the form: 20120405 - r

Apologies for the simple question, but I can't find help for this type of date.
April 5th, 2012 is saved as numeric as "20120405"
How can I convert a vector of such values into usable dates?

You just need the as.Date function:
R> x = "20120405"
R> as.Date(x, "%Y%m%d")
[1] "2012-04-05"
Look at the help file: ?as.Date, but essentially
%Y means year in the form 2012, use %y for 12.
%m is the month.
%d the day.
If your date had separators, say, 2012-04-05, then use something like: %Y-%m-%d. Alternatively, you can use:
R> strptime(x, "%Y%m%d")
[1] "2012-04-05"
In particular, you can pass vectors of dates to these functions, so:
R> y = c("20120405", "20121212")
R> as.Date(y, "%Y%m%d")
[1] "2012-04-05" "2012-12-12"

like this,
(foo <- as.Date("20120405", "%Y%m%d"))
# "2012-04-05"
and maybe you want to format to get the month printed out
format(foo, "%Y %b %d")
# "2012 Apr 05"
You could take a look at this page

With strptime you can convert it to POSIXlt class and with as.Date you can convert it to a Date class using format "%Y%m%d":
strptime( "20120405",format="%Y%m%d")
[1] "2012-04-05"
as.Date( "20120405",format="%Y%m%d")
[1] "2012-04-05"
Edit:
It is not really clear if you have character "20120405" or numeric 20120405. In the latter case you have to convert to character first with as.character(20120405)

You could also use the lubridate package:
library(lubridate)
ymd("20120405")

Related

R function from string cell YEARMonth as date?

So I have this long dataset, where in one column I have a date specified as character in format YYYMMM, but month abbreviated. So for example 1995MAR, 1995APR and so on. How can I transform that to date format?
I tried as.Date but it obviously hasn't worked, and with lubridate::ymd which hasn't worked as well.
Using parse_date_time from lubridate
date <- "1995MAR"
library(lubridate)
parse_date_time(date, order = "Yb")
Output:
[1] "1995-03-01 UTC"
Alternatively using zoo
library(zoo)
as.Date(as.yearmon(date, '%Y%b'))
Output:
"1995-03-01"
str(as.Date(as.yearmon(date, '%Y%b')))
Date[1:1], format: "1995-03-01"
In Base R, add a day number to parse:
date <- "1995MAR"
as.Date(paste(date, "01"), format = "%Y%b %d")
#[1] "1995-03-01"

as.date in R does not convert years properly

I import the csv file:
{casesRaw<-read.csv('CasesRaw.csv')
tail(casesRaw$Date)
my result:
"12/28/2020" "12/29/2020" "12/30/2020" "12/31/2020" "1/1/2021" "1/2/2021"
after conversion:
casesRaw$Date<-as.Date(casesRaw$Date,"%m/%d/%y")
tail(casesRaw$Date)
my result is:
[1] "2020-12-28" "2020-12-29" "2020-12-30" "2020-12-31" "2020-01-01" "2020-01-02"
as you can see still I have 2020-01-01 , ....
Any Idea?
We need %Y for 4-digit year instead of %y which is for 2-digit year
casesRaw$Date <- as.Date(casesRaw$Date, "%m/%d/%Y")
Here is another base R option using gsub
casesRaw$Date <- as.Date(gsub("(.*)/(.*)", "\\2/\\1", casesRaw$Date))

Convert dates to text in R

I have the following dataset with dates (YYYY-MM-DD):
> dates
[1] "20180412" "20180424" "20180506" "20180518" "20180530" "20180611" "20180623" "20180705" "20180717" "20180729"
I want to convert them in:
DD-MMM-YYYY but with the month being text. For example 20180412 should become 12Apr2018
Any suggestion on how to proceed?
M
You can try something like this :
# print today's date
today <- Sys.Date()
format(today, format="%B %d %Y") "June 20 2007"
where The following symbols can be used with the format( ) function to print dates 1
You need to first parse the text strings as Date objects, and then format these Date objects to your liking to have the different text output:
R> library(anytime) ## one easy way to parse dates and times
R> dates <- anydate(c("20180412", "20180424", "20180506", "20180518", "20180530",
+ "20180611", "20180623", "20180705", "20180717", "20180729"))
R> dates
[1] "2018-04-12" "2018-04-24" "2018-05-06" "2018-05-18" "2018-05-30"
[6] "2018-06-11" "2018-06-23" "2018-07-05" "2018-07-17" "2018-07-29"
R>
R> txtdates <- format(dates, "%d%b%Y")
R> txtdates
[1] "12Apr2018" "24Apr2018" "06May2018" "18May2018" "30May2018"
[6] "11Jun2018" "23Jun2018" "05Jul2018" "17Jul2018" "29Jul2018"
R>
You could use the as.Date() and format() functions:
dts <- c("20180412", "20180424", "20180506", "20180518", "20180530",
"20180611", "20180623")
format(as.Date(dts, format = "%Y%m%d"), "%d%b%Y")
More information here
Simply use as.POSIXct and as.format:
dates <- c("20180412", "20180424", "20180506")
format(as.POSIXct(dates, format="%Y%m%d"),format="%d%b%y")
Output:
[1] "12Apr18" "24Apr18" "06May18"

R timeDate drops time in coersion

I have dates of the format: "2/9/2016 21:16"
When I attempt to coerce them to a timeDate, I receive the result: [1] [2016-02-03]
I would prefer to not have to write my own string manipulation, but I can and already have, but there has to be a better way. I have a dataframe and I am attempting to do the following:
restData2 <- restData %>%
mutate(year = year(as.timeDate(Date)),
month = month(as.timeDate(Date)),
day = day(as.timeDate(Date)),
timeCategory = converToTimeCategory(Date)
)
Note, that day is not a function in timeDate either. Day of Week and Day of year exist, I need Day of Month.
The data exists in a data frame. The data is basic transaction data.
David, you are confused. R differentiates between internal representation and actual formated display. For all types.
And there is (once again) no need for timeDate, lubridate, or any other wrapper:
R> intxt <- c("2/9/2016 21:16", "2/11/2016 22:23")
R> parsed <- as.POSIXct(intxt, format="%d/%m/%Y %H:%M")
R> parsed
[1] "2016-09-02 21:16:00 CDT" "2016-11-02 22:23:00 CDT"
R> format(parsed, "%d %b %Y at %H:%M")
[1] "02 Sep 2016 at 21:16" "02 Nov 2016 at 22:23"
R>
Here we parse a datetime object into the standard POSIXct, specifying a format. Which can be day-month or month-day; here I picked the former.
Given the parsed object, I first show the default display, and then a custom format string.
Lastly, if you must, you can also convert to timeDate:
R> library(timeDate)
R> as.timeDate(parsed)
GMT
[1] [2016-09-03 02:16:00] [2016-11-03 03:23:00]
R>
Not the timezone adjustment from my local (Central) time.

R Strptime Year and Month with No Delimiter returning NA

I'm probably doing something stupid and not seeing it, but:
> strptime("201101","%Y%m")
[1] NA
From help strptime:
%Y Year with century
%m Month as decimal number (01–12)
Just paste a day field (say, "01") that you ignore:
R> shortdate <- "201101"
R> as.Date(paste(shortdate, "01", sep=""), "%Y%m%d")
[1] "2011-01-01"
R>
I prefer as.Date() for dates and strptime() for POSIXct objects, i.e. dates and times.
You can then convert the parsed Date object into a POSIXlt object to retrieve year and month:
R> mydt <- as.Date(paste(shortdate, "01", sep=""), "%Y%m%d")
R> myp <- as.POSIXlt(mydt)
R> c(myp$year, myp$mon)
[1] 111 0
R>
This is standard POSIX behaviour with years as "year - 1900" and months as zero-indexed.
Edit seven years later: For completeness, and as someone just upvoted this, the functions in my anytime package can help:
R> anytime::anydate("201101") ## returns a Date
[1] "2011-01-01"
R> anytime::anytime("201101") ## returns a Datetime
[1] "2011-01-01 CST"
R>
The use a different parser (from Boost Date_time which is more generous and imputes the missing day (or day/hour/minute/second in the second case).

Resources