I am trying to force dates from two formats to one, which I can easily transfer to as.Date. Here's a sample:
library(dplyr)
df <- data_frame(date = c("Mar 29 2017 9:30AM", "5/4/2016"))
I tried this:
df %>%
mutate(date = gsub("([A-z]{3}) (\\d{2}) (\\d{4}).*",
paste0(which(month.abb == "\\1"),"/\\2","/\\3"), date))
But this gave me the following:
date
1 /29/2017
2 5/4/2016
but i want it!
date
1 3/29/2017
2 5/4/2016
It seems that when I use it month.abb == "\\1", it does not use the capture group output ( "Mar"), it just uses the caller's text ( "\\1"). I want to do this in regex, if possible. I know you can do it differently, but want to be smooth.
Any ideas?
Zafar source
share