How to Convert Month-Year String to Date in R

Converting year and month ( yyyy-mm format) to a date?

Try this. (Here we use text=Lines to keep the example self contained but in reality we would replace it with the file name.)

Lines <- "2009-01  12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386"

library(zoo)
z <- read.zoo(text = Lines, FUN = as.yearmon)
plot(z)

The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of ?plot.zoo .

The zoo series, z, that is created above has a "yearmon" time index and looks like this:

> z
Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Aug 2009 Sep 2009 Oct 2009
12 310 2379 234 14 1 34 2386

"yearmon" can be used alone as well:

> as.yearmon("2000-03")
[1] "Mar 2000"

Note:

  1. "yearmon" class objects sort in calendar order.

  2. This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of z to "Date" class: time(z) <- as.Date(time(z)) .

How can I convert a Month Year string to a date object in R?

We need a day as well to create the Date

df$Date <- as.Date(paste(df$Date, 1), format = "%B %Y %d")

Convert month year to a date in r

You can paste 01 to the vector using paste and then convert to date by specifying the appropriate format

as.Date(paste('01', v1), format='%d %b %Y')
#[1] "2009-02-01" "2010-01-01", "2011-03-01"

data

v1 <- c("Feb 2009", "Jan 2010", "Mar 2011")

Convert string into date format in R

The as.Date() function will convert a string into date format, and the format of the output will always be in yyyy-mm-dd format in R (ISO 8601). The format argument in the as.Date() function is to specify the date format of the string input. I remember I initially thought it was specifying the output format, but it's the input format (you can change the output format with a subsequent format() function, however this will convert it back to a string).

Your string looks to be in ddmmyyyy (%d%m%Y) format, this should be what you specify as the format argument in as.Date(). Your format does not include hyphens, so the format argument should also not include hyphens. Note that ddmmyyyy, dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy are all different date formats, even though the day, month and year are in the same order, they would be converted to date with formats %d%m%Y, %d-%m-%Y, %d/%m/%Y, and %d.%m.%Y, respectively.

Further advice on working with dates and times is available in the relevant chapters of R for Data Science by Wickham & Grolemund and The R Cookbook by Teetor & Long.

Convert a string into dates using R

i used paste0() instead of sprintf, but it seems it works.

> x<-paste0("010",11960)
> x
[1] "01011960"
> as.Date(x , format = "%d%m%Y" )
[1] "1960-01-01"

EDIT for 2 digit months i use ifelse() and nchar()

y<-c(11960,11970,11980, 111960,111970,111980)
x<-ifelse(nchar(y) == 5,paste0("010",y),paste0("01",y))

> x
[1] "01011960" "01011970" "01011980" "01111960" "01111970" "01111980"

as.Date(x , format = "%d%m%Y" )
[1] "1960-01-01" "1970-01-01" "1980-01-01" "1960-11-01" "1970-11-01" "1980-11-01"

Converting a date 'year - month - date' to only 'year and month' in r with SQL data

Up front, your attempt of as.Date(df$Posting_Date, format="%Y %m") seems backwards: the function as.Date is for converting from a string to a Date-class, and its format= argument is to identify how to find the year/month/day components of the string, not how you want to convert it later. (Note that in R, a Date is shown as YYYY-MM-DD. Always. Telling R you want a date to be just year/month is saying that you want to convert it to a string, no longer date-like or number-like. lubridate and perhaps other packages allow you to have similar-to-Date like objects.)

For df, one can just subset the strings without parsing to Date-class:

substring(df$Posting_Date, 1, 7)
# [1] "2020-05" "2020-10" "2021-10"

If you want to do anything number-like to them, you can convert to Date-class first, and then use format(.) to convert to a string with a specific format.

as.Date(df$Posting_Date)
# [1] "2020-05-28" "2020-10-09" "2021-10-19"
format(as.Date(df$Posting_Date), format = "%Y-%m")
# [1] "2020-05" "2020-10" "2021-10"

For df2, though, since it is numeric you need to specify an origin= instead of a format=. I'm inferring that these are based off of epoch, so

as.Date(df2$Posting_Date, origin = "1970-01-01")
# [1] "2020-05-28" "2020-10-09" "2021-10-19"
format(as.Date(df2$Posting_Date, origin = "1970-01-01"), format = "%Y-%m")
# [1] "2020-05" "2020-10" "2021-10"

Note that R stores Date (and POSIXct, incidentally) as numbers internally:

dput(as.Date(df2$Posting_Date, origin = "1970-01-01"))
# structure(c(18410, 18544, 18919), class = "Date")


Related Topics



Leave a reply



Submit