Home > Software engineering >  Finding months within a column of character values
Finding months within a column of character values

Time:10-11

I am working in R and have a dataframe with a column of dates that are stored as character variables in the format (YYYY-MM-DD). I want to subset my dataframe with only events that occurred in the month of February. Any help would be appreciated.

CodePudding user response:

Convert to Date class and subset based on the month format

subset(df1, format(as.Date(date), '%b') == 'Feb')

CodePudding user response:

Using the lubridate package, we convert your column to Date class and extract the month with month():

library(lubridate)
your_data$date_column = as.Date(your_data$date_column)
subset(your_data, month(your_data$date_column) == 2)
  •  Tags:  
  • r
  • Related