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.
Related
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 12 months ago.
This question has appeared before, I know, but I haven't been able to derive the correct output after running Sys.setlocale("LC_TIME", "american").
Ok, so I have dataframe with a column of dates that look something like "October 2020" and "June 2021", etc.
I run as.Date(dataframe$month_column, format = "%B %Y"), and I do this after running Sys.setlocale() with the above inputs. But my output continues to be a bunch of NAs.
I suppose one work around I could try would be to split the Month and Year parts by the space into two columns, which might make it easier to coerce into date types, but I'd like to avoid that if possible, since I want to plot quantities by Month Year.
Any insight would be appreciated.
You can convert character strings of month and years with the my() function in the lubridate package. my, in this case, stands for month-year (there are also other functions, like mdy() which look for entries in the form of month-day-year, and dym() which expect day-month-year order).
library(lubridate)
dates <- data.frame(
dt = c("October 2020", "June 2021")
)
dates$converted <-my(dates$dt)
dates
Output:
dt converted
1 October 2020 2020-10-01
2 June 2021 2021-06-01
This question already has an answer here:
Generating a date from a string with a 'Month-Year' format
(1 answer)
Closed 4 years ago.
I have a list of program dates as character strings in the following format
program.date.have <-c('Sep-14','Aug-14','Sep-16')
I am assuming that all these programs started on the first day of each month, and I want the program.date to end up like
program.date.want<-c('2014-09-01', '2014-08-01, '2016-09-01') or in YYYY-MM-DD format.
To start somewhere I have decided to covert the character strings into the date format in the following way
program.date.have<-c('Sep-14','Aug-14','Sep-16')
betterDates <- as.Date(program.date,
format = "%m-%y")
But even that does not seem to work. how do I use values in program.date variable to be converted into format I want in program.date.want
We can use as.yearmon from zoo, specify the format, and wrap with as.Date which automatically generates the 'day' as the first of the month.
library(zoo)
as.Date(as.yearmon(program.date.have, "%b-%y"))
#[1] "2014-09-01" "2014-08-01" "2016-09-01"
Or a base R option is to paste the '01' at the start or end and then specify the appropriate format in as.Date
as.Date(paste0(program.date.have, "-01"), "%b-%y-%d")
#[1] "2014-09-01" "2014-08-01" "2016-09-01"
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"
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 9 years ago.
I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01
Thanks
I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.
As date:
library(zoo)
as.yearmon("200101", "%Y%m")
## > as.yearmon("200101", "%Y%m")
## [1] "Jan 2001"
As character:
gsub("([0-9]{4})","\\1-", "200101")
## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"
## gsub("([0-9]{4})","\\1/", "200101")
This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks
It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf
strptime(data$dob, "%Y/%m/%d")