我的数据来自excel。日期采用dd / mm / yyyy格式:
certificado$fecha <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019",
"18/09/2019")
但是,R正在读取某些日期,例如mm / dd / yyyy。我的代码应该将它们全部转换为特定格式。
certificados$Fecha <- as.Date(certificados$Fecha,format = "%d/%m/%Y")
但是由于日期格式问题,我无法获得NAs。
答案 0 :(得分:1)
如果您无法从源头上解决此问题,则此代码将找到两种格式:
vec <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019", "18/09/2019")
out <- as.Date(vec, format = "%d/%m/%Y")
out
# [1] "2019-02-22" NA "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"
isna <- is.na(out)
out[isna] <- as.Date(as.integer(vec[isna]), origin = "1900-01-01")
out
# [1] "2019-02-22" "2019-08-04" "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"