Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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, 
-6L))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading