I am trying to incorporate date in a column. But it is returning number since NA is present. Can anyone help me?
asdd <- data.frame(a = c(NA,as.Date(today(),"%m/%d/%Y")), b = c(2,3))
asdd
a b
1 NA 2
2 19220 3
i want get as Date in the column?
CodePudding user response:
Place the as.Date
before c
and not inside c
.
data.frame(a = as.Date(c(NA, "1970-01-01")), b = c(2,3))
# a b
#1 <NA> 2
#2 1970-01-01 3
CodePudding user response:
lubridate::today()
returns a character, same as Sys.Date()
. But in the absence of a date in the first cell data.frame()
will interprete this NA
as a number. So use as.character()
and outside the vector as.Date()
.
d <- data.frame(a = as.Date(c(NA,as.character(lubridate::today()))),
b = c(2,2))
d
returns:
a b
<date> <dbl>
<NA> 2
2022-08-16 2