Project date Re-editing group outputs in R

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?

+4
source share
1 answer

Here is one way: gsubfn

library(gsubfn)
df$date <- gsubfn("^([A-Za-z]{3})\\s+(\\d{2})\\s+(\\d{4}).*", function(x, y, z) 
                  paste(match(x, month.abb),y, z, sep="/"), df$date)
df$date
#[1] "3/29/2017" "5/4/2016" 

Or subcombined withgsubfn

sub("(\\S+)\\s+(\\S+)\\s+(\\S+).*", "\\1/\\2/\\3", 
      gsubfn("^([A-z]{3})", setNames(as.list(1:12), month.abb), df$date))
#[1] "3/29/2017" "5/4/2016" 
+1
source

Source: https://habr.com/ru/post/1675657/


All Articles