I have a large dataset and one of the columns includes dates formatted DD/MM/YYYY and I would like to just have MM/YYYY. Is there a way to apply this to the entire column in the dataset dat$date
?
Asked
Active
Viewed 1,080 times
1 Answers
1
We can convert to Date
class first with as.Date
and then use format
dat$date <- format(as.Date(dat$date, "%d/%m/%Y"), "%m/%Y")
Or another option is regex to match the digits (\\d+
) from the start (^
) of the string followed by /
and replace with blank (''
)
dat$date <- sub("^\\d+\\/", "", dat$date)
data
dat <- data.frame(date = c('05/10/2015', '15/05/2010'), stringsAsFactors = FALSE)

akrun
- 874,273
- 37
- 540
- 662
-
The second option worked better but it gave me the day instead of the month is there a way to get the month instead? – MCM Apr 27 '20 at 18:37
-
@MCM you said the format is DD/MM/YYYY so it is removing the DD/ – akrun Apr 27 '20 at 18:38