Convert "Jan.2008" to date variable - r

How would I convert the following character variables to dates?
strDates <- c("Jan.2008", "Feb.2008")
str(strDates)
chr [1:2] "Jan.2008" "Feb.2008"
dates <- as.Date(strDates, "%b %Y")
str(dates)
Date[1:2], format: NA NA
Any assistance would be greatly appreciated

To form a valid 'date', you also need a day which your data was lacking. So we add one, and we simply use an arbitrary day (here: first of the month):
R> strDates <- c("Jan.2008", "Feb.2008")
R> strptime(paste("01", strDates), "%d %b.%Y")
[1] "2008-01-01" "2008-02-01"
R>

A Date requires a day element as well, so you can add that to the input string with paste:
full.dates <- paste("01", strDates, sep = ".")
Specify the template correctly, including separator tokens:
as.Date(full.dates, "%d.%b.%Y")
[1] "2008-01-01" "2008-02-01"

Related

Convert factor(non-uniform) to date in R?

I have dates in this format:
Apr-12,
Dec-12,
30-Jul-14,
Mar-16,
29-Feb-16,
May-17,
20-Nov-14,
R is treating it like factor variable. I want it to treat it like a date, and wherever the day of the date is missing, it should replace it with 1st.
Thank you in advance!
I think we need to parse them separately because the format is not consistent. We first parse the ones which have date, month and year component. The ones which return NA's are then parsed by adding "01" in them.
new_x <- as.Date(x, "%d-%b-%y")
new_x[is.na(new_x)] <- as.Date(paste0("01-", x[is.na(new_x)]), "%d-%b-%y")
new_x
#[1] "2012-04-01" "2012-12-01" "2014-07-30" "2016-03-01" "2016-02-29" "2017-05-01"
#[7] "2014-11-20"
Read more about formats at ?strptime.
data
x <-factor(c("Apr-12", "Dec-12", "30-Jul-14", "Mar-16", "29-Feb-16",
"May-17","20-Nov-14"))
Conditionally append a "01-" when the first three characters are not in the system vector, month.abb
as.Date( ifelse( substr(dtvec,1,3) %in% month.abb, paste0("01-",dtvec), dtvec) ,"%d-%b-%y")
[1] "2012-04-01" "2012-12-01" "2014-07-30" "2016-03-01" "2016-02-29" "2017-05-01" "2014-11-20"

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"

how to split numbers

I have a date format like this
5170301, where it means 1st March 2017.And I have 5 attached to it
I want the format of the date to be changed.
So can anyone help me in splitting that 5 from the date?
We can use substring to read from the 2nd character onwards
v1 <- substring(df1$date, 2)
NOTE: It should work for numeric/character/factor class
Then we change it to Date class
v2 <- as.Date(v1, "%y%m%d")
and if needed change the format
format(v2, "%d %b %Y")
Or as #thelatemail mentioned, it can be mentioned in the format
as.Date(df1$date, "5%y%m%d")
You can split it quite nicely with the stringr package
Split <- stringr::str_split_fixed(string=Column_Name, pattern="5", n=2)
This will give two variables: one blank and one of your value after the "5" (170301)
Then can change it to the date as so:
Date1 <- as.Date(format="%d%m%y", x = Split)

How to convert a date to YYYYDDD?

I can't figure out how to turn Sys.Date() into a number in the format YYYYDDD. Where DDD is the day of the year, i.e. Jan 1 would be 2016001 Dec 31 would be 2016365
Date <- Sys.Date() ## The Variable Date is created as 2016-01-01
SomeFunction(Date) ## Returns 2016001
You can just use the format function as follows:
format(Date, '%Y%j')
which gives:
[1] "2016161" "2016162" "2016163"
If you want to format it in other ways, see ?strptime for all the possible options.
Alternatively, you could use the year and yday functions from the data.table or lubridate packages and paste them together with paste0:
library(data.table) # or: library(lubridate)
paste0(year(Date), yday(Date))
which will give you the same result.
The values that are returned by both options are of class character. Wrap the above solutions in as.numeric() to get real numbers.
Used data:
> Date <- Sys.Date() + 1:3
> Date
[1] "2016-06-09" "2016-06-10" "2016-06-11"
> class(Date)
[1] "Date"
Here's one option with lubridate:
library(lubridate)
x <- Sys.Date()
#[1] "2016-06-08"
paste0(year(x),yday(x))
#[1] "2016160"
This should work for creating a new column with the specified date format:
Date <- Sys.Date
df$Month_Yr <- format(as.Date(df$Date), "%Y%d")
But, especially when working with larger data sets, it is easier to do the following:
library(data.table)
setDT(df)[,NewDate := format(as.Date(Date), "%Y%d"
Hope this helps. May have to tinker if you only want one value and are not working with a data set.

Date in the form: 20120405

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")

Resources