2

The codes and results shown below:

(p <- as.POSIXct(c("2017-09-21 UTC", "2017-09-22 UTC")))
[1] "2017-09-21 PDT" "2017-09-22 PDT"

as.Date(p)
[1] "2017-09-21" "2017-09-22"

(d <- sapply(p, as.Date))
[1] 17430 17431

Why the sapply returns a numeric vector?

R version 3.4.1 (2017-06-30)

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
A-L
  • 41
  • 2
  • 2
    This is due to the `simplify=TRUE` defualt argument, that simplifies it to an array / matrix which can't hold date class `matrix(as.Date(p))` – user20650 Oct 18 '17 at 19:59
  • 1
    *If simplification occurs* (and it does here), *the output type is determined from the highest type of the return values in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression, after coercion of pairlists to lists.* - `help(sapply)` – Rich Scriven Oct 18 '17 at 20:00
  • @user20650 That looks worth posting as an answer. – Frank Oct 18 '17 at 20:06

0 Answers0