Extract month and year while ignoring day from column in R

I have a dataframe with a column named "Start" which has dates formatted as "m/d/yyyy" which I want to use to create a new column with just "m/yyyy". I have looked at other questions here but most don’t follow this exact format and solutions haven’t worked for this.

Here is an example of the head of my original data column:

> head(data$Start)
[1] "3/6/2023"  "3/6/2023"  "3/6/2023"  "3/6/2023"  "1/17/2023" "1/9/2023" 

I tried adding a new column with just the month and year using the following code:

data$MthYr <- format(as.Date(data$Start), "%m/%Y")

This returns this:

> head(data$MthYr)
[1] "06/0003" "06/0003" "06/0003" "06/0003" NA        "09/0001"

It appears to just be using the day as the month and the month as the year and adding in leading zeros. How can this be fixed?

>Solution :

Use the format in as.Date for conversion. The default format used is %Y-%m-%d

format(as.Date(data$Start, "%m/%d/%Y"), "%m/%Y")
[1] "03/2023" "03/2023" "03/2023" "03/2023" "01/2023" "01/2023"


data <- structure(list(Start = c("3/6/2023", "3/6/2023", "3/6/2023", 
"3/6/2023", "1/17/2023", "1/9/2023")), class = "data.frame", row.names = c(NA, 

Leave a Reply