Мы можем использовать anydate
из anytime
library(dplyr)
library(anytime)
DF1 <- DF %>%
mutate_at(vars(START, END), anydate)
DF1
# KEY START END
#1 12 2019-01-03 2019-01-10
#2 55 2018-12-31 2019-01-05
#3 889 2019-01-07 2019-01-09
#4 896 2019-01-08 2019-01-12
str(DF1)
#'data.frame': 4 obs. of 3 variables:
# $ KEY : num 12 55 889 896
# $ START: Date, format: "2019-01-03" "2018-12-31" "2019-01-07" "2019-01-08"
# $ END : Date, format: "2019-01-10" "2019-01-05" "2019-01-09" "2019-01-12"
В devel
версии dplyr
мы можем объединить mutate
с across
DF %>%
mutate(across(c(START, END), anydate))
# KEY START END
#1 12 2019-01-03 2019-01-10
#2 55 2018-12-31 2019-01-05
#3 889 2019-01-07 2019-01-09
#4 896 2019-01-08 2019-01-12
данные
DF <- structure(list(KEY = c(12, 55, 889, 896), START = structure(c(2L,
1L, 3L, 4L), .Label = c("2018-12-31T12: 03: 13", "2019-01-03T16: 59: 51",
"2019-01-07", "2019-01-08"), class = "factor"), END = structure(c(3L,
1L, 2L, 4L), .Label = c("2019-01-05T12: 00: 00", "2019-01-09",
"2019-01-10T13: 10: 00", "2019-01-12"), class = "factor")),
class = "data.frame", row.names = c(NA,
-4L))