R format of date trouble - r

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"

Related

How to convert full date character into mm/dd/yyyy in R?

I have a dataset in R with a date column that is currently being treated as a character column. The dates in this column are listed as "August 24 2012" and I am trying to convert it into a date format such as 08/24/2012.
I am a novice with R and have tried to use format() and lubridate with no success. How do I convert these dates from a character to date?
We need to convert it to Date class and then use format
format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"
As the order is month, day, year use mdy from lubridate
library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"
It is based on the order, e.g.
ydm("2012 24 August")
#[1] "2012-08-24"
data
dates <- "August 24 2012"

Unable to convert Month-Year string to Date in R

I'm using as.Date to convert a string like Aug-2002 to a dates object representing just the month of Aug of 2002, or if a day must be specified, Aug 1, 2002.
However
> as.Date(c('07-2002'), "%M-%Y")
[1] "2002-11-06"
> as.Date(c('Aug-2002'), "%b-%Y")
[1] NA
Why does the first line of code convert it to a different month and day? And the second one is NA?
I referred to this table for the formatting symbols.
The problem you are having is that the dates you have do not have a day value. Without the day value the format="%m-%Y" will not work in as.Date. These options below will solve them:
as.Date(paste0('01-', c('07-2002')), format="%d-%m-%Y")
library(zoo) #this is a little more forgiving:
as.yearmon(c('07-2002'), "%m-%Y")
as.yearmon(c('Aug-2002'), "%b-%Y")
as.Date(as.yearmon(c('07-2002'), "%m-%Y"))

How can I convert this to month?

6/9/2013 1:15
7/9/2013 1:15
I have a series of data in a column of this format.
I am trying to convert it into a month and store it in a different column.
How can I do it?
Intended output
June
July
I tried using the lubridate library but not able to.
How about
x <- c("6/9/2013 1:15","7/9/2013 1:15")
strftime(as.Date(x, format="%m/%d/%Y %H:%M"), "%B")
# [1] "June" "July"
First convert to a proper date via as.Date() then use the formatting options in strftime to get the month name.

Converting between date formats in R?

I have a data.frame (CSV originally) in R with dates are in the following 3 formats:
2011-06-02T17:16:05Z
2012-06-02T17:16:05-07:00
6/2/11 17:16:05
which is year-month-day-time. I don't quite know what the -07:00 is, as it seems to be the same for all timestamps (except for some where it is -08:00), but I guess it's some type of time zone offset.
I am not quite sure what format these are (does anyone know?), but I need to convert it to this format:
6/2/11 17:16:05
which is year-month-day-time
I would like to do this in such a way so that all the dates in the CSV (in one and the same row) is converted to the second format. How can I accomplish this in R?
The full dataset can be found here.
Here's another attempt, assuming your data is text to start with:
test <- c("2011-06-02T17:16:05Z","2012-06-02T17:16:05-07:00")
format(as.POSIXct(test,format="%Y-%m-%dT17:%H:%M"),"%m/%d/%y %H:%M")
[1] "06/02/11 16:05" "06/02/12 16:05"
You can try the following, where myDates would be the column of dates
format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%Y %H:%M")
[1] "06/02/2011 16:05" "06/02/2012 16:05"
or with 2-digit year
# Note the lower-case %y at the end
format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%y %H:%M")
[1] "06/02/11 16:05" "06/02/12 16:05"
As for the Z, that indicates GMT (think: London).
the -7:00 indicates 7 hours back from GMT (think: Colorado / MST etc)
Please see here for more reference

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