You could use regular expressions with stringr
and lubridate
:
df <- data.frame(range = c("1/1/13 to 12/31/13",
"5/5/15 to 10/27/15"))
df$from <- lubridate::mdy(stringr::str_extract(df$range,"^.*?(?=to)"))
df$to <- lubridate::mdy(stringr::str_extract(df$range,"(?=to).*?$"))
df
#> range from to
#> 1 1/1/13 to 12/31/13 2013-01-01 2013-12-31
#> 2 5/5/15 to 10/27/15 2015-05-05 2015-10-27
Created on 2020-09-20 by the reprex package (v0.3.0)
Or without converting to date :
library(dplyr)
df <- data.frame(range = c("1/1/13 to 12/31/13",
"5/5/15 to 10/27/15",
"October to November"))
df %>% mutate(from = stringr::str_extract(range,"^.*?(?= to)"),
to = stringr::str_extract(range,"(?<=to ).*?$"))
#> range from to
#> 1 1/1/13 to 12/31/13 1/1/13 12/31/13
#> 2 5/5/15 to 10/27/15 5/5/15 10/27/15
#> 3 October to November October November
Created on 2020-09-20 by the reprex package (v0.3.0)