Turning date strings into usable date objects in R - r

My strings look like:
[1] "Sunday, April 10, 2016" "Saturday, April 16, 2016"
I would like to apply an algorithm in R so they each read something like this and have a Class POSIXlt or POSIXct:
[1] "04/10/2016" "04/16/2016"
I tried to use strptime and as.Date functions, but I just cannot find a good way of doing this automatically without first removing the day of the week up front.
Any and all solutions are appreciated! I know many of you are R gurus out there, and I would very much appreciate your help!
Thank you.

It is all in help(strptime):
R> d <- c("Sunday, April 10, 2016", "Saturday, April 16, 2016")
R> as.Date(d, "%A, %B %d, %Y")
[1] "2016-04-10" "2016-04-16"
R>
Note that the result of as.Date() returns a Date object with which you can compute properly: make chanages, add/subtract, compare, convert and format differently for output if needed.

Related

Can't use zoo's as.yearmon properly, the transformed column has years showing up like 0086 instead of 1986

Hello and thanks for being here.
I'm trying to convert dates with as.yearmon but the results I'm getting are odd and I do not know how to fix this; I tried searching on here and on the offical package guide without finding someone with the same problem.
The problem is that I transformed a column of a dataset which was formatted as "month/year" with as.yearmon but the results were not correct.
For example, the first 3 values of the column of the original DF are: "1/86", "2/86", "3/86".
After using this function to convert them:
library(zoo)
Dates <- Returns
Dates$Month<- zoo::as.yearmon(Dates$Month, "%m / %Y")
[Where "Returns" is the original dataframe and Dates the new one with the modified dates.]
The result I got, instead of being: "gen 1986", "feb 1986", "mar 1986" was "gen 0086", "feb 0086", "mar 0086" and I don know why.
[I should not that "gen", "feb", "mar" are in Italian; I do not know if that matters and I do not know how to change that to "Jan", "Feb", "Mar" which I think I'll have to do as well]
Thanks in advance for your help, if something is not clear just let me know; I'm still a rookie.
You need to use "%m/%y" instead of "%m/%Y". Your dates don't have a the full year notation.
x <- c("1/86", "2/86", "3/86")
zoo::as.yearmon(x, "%m/%y")
[1] "jan 1986" "feb 1986" "mrt 1986"
Date functions return the names in the local locale. If you want them in English:
Sys.setlocale("LC_TIME", "English")
zoo::as.yearmon(x, "%m/%y")
[1] "Jan 1986" "Feb 1986" "Mar 1986"
Everytime you restart R, this will be set back to your locale.
More info here on SO

R lubridate. What goes wrong?

Tried to make a factor to date.
X:
Jul-15
Jul-15
Jul-15
Jul-15
Aug-16
I want to convert them to a date.
X <- as.Date(Xx, format="%b-%y")
Results in NA's.
This was my source for the code: https://www.r-bloggers.com/date-formats-in-r/
as.Date is the base R function, as_date is the lubridate function, but this is not your problem
Without a day, you're not completely specifying the date completely. Unlike year, where R will fill in with the current year, this does not appear to happen for a missing day
As a workaround, run:
X <- as.Date(paste0(Xx,'-01'), format="%b-%y-%d")
Which will set the dates to be the first of the month
You need a complete date, with day :
library(lubridate)
X <- "Jul-15"
X <- paste("01", X, sep="-")
dmy(X)
The yearmon function from the zoo package will help you here. Since you have lubridate as a tag on the question, we can then use as_date from the lubridate package to turn it into a date object. The base function as.Date will work as well.
library(zoo)
library(lubridate)
as_date(as.yearmon(Xx, "%b-%y"))
[1] "2015-07-01" "2015-07-01" "2015-07-01" "2015-07-01" "2016-08-01"
If you want to leave them as yearmon class, you can just run:
as.yearmon(Xx, "%b-%y")
[1] "Jul 2015" "Jul 2015" "Jul 2015" "Jul 2015" "Aug 2016"

Missing months sorting df by Year and Month using as.yearmon() from zoo-package

I have problems sorting data by month and year. I'm using as.yearmon() as shown here. However, some months are not recognized by as.yearmon function (march, mai, october, december). I found out that using German abbreviations instead of English abbreviations works fine, for example Dez instead od Dec. Also tried caps and no caps. It seems to by a default setting or I miss a command in the as.yearmon function. Ideas?? Here some sample data:
dat <- c("2009-Sep","2009-Feb","2009-Jan","2009-Oct")
require(zoo)
d2 <- as.yearmon(dat, "%Y-%b")
sort(d2)
In this case I lose the last entry 2009-Oct and I don't know why. My output is only:
"Jan 2009" "Feb 2009" "Sep 2009"
However, using this (note Okt instead of Oct, german style):
dat <- c("2009-Sep","2009-Feb","2009-Jan","2009-Okt")
I get the wright output. Do you have ideas how to solve this problem? Thanks
You possibly need to change the locale settings:
Sys.setlocale("LC_TIME", "C")
If that does not work you can start R with "LANG="C"

R format of date trouble

I'm confused as to why this as.Date("201410", "%Y%m") is not converted to a date... That is, I expect that the format article of the function as.Date in the example would take the "201410" and convert it to date.
Any help?
If you are willing to make the assumption that these dates are on average occurring at the middle of a month then this would be a dodgy way of using the Date class:
as.Date(paste0("201410", "01"), "%Y%m%d")
#[1] "2014-10-01"
This is how to create a yearmon object:
> require(zoo)
> as.yearmon("201410","%Y%m")
[1] "Oct 2014"

Formatting month abbreviations using as.Date [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
I'm working with monthly data and have a character vector of dates, formatted:
Sep/2012
Aug/2012
Jul/2012
and so on, back to 1981. I've tried using
as.Date(dates, "%b/%Y")
where %b represents month abbreviations, but this only returns NAs. What am I doing wrong?
Note: I already found a workaround using gsub() to add "01/" in front of each entry, like so:
01/Sep/2012
01/Aug/2012
01/Jul/2012
Then as.Dates() works, but this seems a little inelegant, and isn't strictly accurate anyway.
You are looking for as.yearmon() in the zoo package. Given your dates
dates <- c("Sep/2012","Aug/2012","Jul/2012")
we load the package and convert to the "yearmon" class
require(zoo)
dates1 <- as.yearmon(dates, format = "%b/%Y")
dates1
Which gives
R> dates1
[1] "Sep 2012" "Aug 2012" "Jul 2012"
You can coerce to an object of class "Date" using the as.Date() method
R> as.Date(dates1)
[1] "2012-09-01" "2012-08-01" "2012-07-01"
Which would be a simpler way of getting the thing you did via gsub(). There is a frac argument which controls how far through the month the day component should be:
R> as.Date(dates1, frac = 0.5)
[1] "2012-09-15" "2012-08-16" "2012-07-16"
But that may ont be sufficient for you.
If you really only want the dates stored as you have them, then they aren't really dates but if you are happy to work within the zoo package then the "yearmon" class can be used as an index for a zoo object which is a time series.

Resources