I have a large data set with many columns containing dates in two different formats:
\"1996-01-04\" \"1996-01-05\" \"1996-01-08\" \"1996-01-09\" \"1996-01-10
df <- data.frame(X1 = c("1996-01-04", "1996-01-05", "1996-01-08", "1996-01-09", "1996-01-10", "1996-01-11"), X2 = c("02/01/1996", "03/01/1996", "04/01/1996", "05/01/1996", "08/01/1996", "09/01/1996"), stringsAsFactors=F)
'data.frame': 6 obs. of 2 variables:
$ X1: chr "1996-01-04" "1996-01-05" "1996-01-08" "1996-01-09" ...
$ X2: chr "02/01/1996" "03/01/1996" "04/01/1996" "05/01/1996" ...
library(dplyr)
library(lubridate)
ans <- df %>%
mutate(X1 = ymd(X1), X2 = mdy(X2))
X1 X2
1 1996-01-04 1996-02-01
2 1996-01-05 1996-03-01
3 1996-01-08 1996-04-01
4 1996-01-09 1996-05-01
5 1996-01-10 1996-08-01
6 1996-01-11 1996-09-01
str(ans)
'data.frame': 6 obs. of 2 variables:
$ X1: Date, format: "1996-01-04" "1996-01-05" ...
$ X2: Date, format: "1996-02-01" "1996-03-01" ...