Convert Month Year to a 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)) .

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

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

convert year and month into date format

You need to paste then put format date. Something like this:

dates <- c("192607", "192608")
dates <- paste0(dates,"01")
dates <- as.Date(dates, format ="%Y%m%d")
dates

The result is

[1] "1926-07-01" "1926-08-01"

Extract Month and Year From Date in R

This will add a new column to your data.frame with the specified format.

df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")

df
#> ID Date Month_Yr
#> 1 1 2004-02-06 2004-02
#> 2 2 2006-03-14 2006-03
#> 3 3 2007-07-16 2007-07

# your data sample
df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )

a simple example:

dates <- "2004-02-06"

format(as.Date(dates), "%Y-%m")
> "2004-02"

side note:
the data.table approach can be quite faster in case you're working with a big dataset.

library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]

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 a column having Abbreviated Month-Year format to Date Type

Sum up the comments so far:

vec <- c("Nov - 16", "Apr - 18")
(o1 <- as.Date(paste("01", vec), format = "%d %b - %y"))
# [1] "2016-11-01" "2018-04-01"
(o2 <- lubridate::dmy(paste("01", vec)))
# [1] "2016-11-01" "2018-04-01"
(o3 <- zoo::as.yearmon(vec, "%b - %y"))
# [1] "Nov 2016" "Apr 2018"

It should be noted that the first two produce objects of class Date, and the third returns class yearmon, and their relative numeric values are a bit different:

dput(o1)
# structure(c(17106, 17622), class = "Date")
dput(o2)
# structure(c(17106, 17622), class = "Date")
dput(o3)
# structure(c(2016.83333333333, 2018.25), class = "yearmon")

though you can always convert from the third if need be, as suggested by @RonakShah.

as.Date(o3)
# [1] "2016-11-01" "2018-04-01"

How to extract Month from date in R

?month states:

Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon,
yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts
objects.

Your object is a factor, not even a character vector (presumably because of stringsAsFactors = TRUE). You have to convert your vector to some datetime class, for instance to POSIXlt:

library(lubridate)
some_date <- c("01/02/1979", "03/04/1980")
month(as.POSIXlt(some_date, format="%d/%m/%Y"))
[1] 2 4

There's also a convenience function dmy, that can do the same (tip proposed by @Henrik):

month(dmy(some_date))
[1] 2 4

Going even further, @IShouldBuyABoat gives another hint that dd/mm/yyyy character formats are accepted without any explicit casting:

month(some_date)
[1] 2 4

For a list of formats, see ?strptime. You'll find that "standard unambiguous format" stands for

The default formats follow the rules of the ISO 8601 international
standard which expresses a day as "2001-02-28" and a time as
"14:01:02" using leading zeroes as here.



Related Topics



Leave a reply



Submit