2

Will vapply work with data classes beyond numeric, character, and logical? Can vapply return as.Date data in the original class? Here's my list:

name <- "Truman"
birth <- as.Date("1884/05/08")
death <- as.Date("1972/12/26")
no33 <-  list("name"=name"birth"=birth,"death"=death)


name <- "Eisenhower"
birth <- as.Date("1890/10/14")
death <- as.Date("1969/03/28")
no34 <- list("name"=name"birth"=birth,"death"=death)

presidents <- list(no33,no34)

lapply works fine, but sapply transforms the dates:

lapply(1:length(presidents), FUN= function (x) presidents[[x]]$death)

sapply(1:length(presidents), FUN= function (x) presidents[[x]]$death)

vapply throws an error when specifying Date as data type:

vapply(1:length(presidents), FUN= function (x)
  as.character(presidents[[x]]$death), FUN.VALUE = date(1)))

I've got this one line work around

as.Date(vapply(1:length(presidents), FUN= function (x)
 as.character(presidents[[x]]$death), FUN.VALUE = 
 character(1)))

What is sapply doing? And is there a better alternative to my one-line hack?

M--
  • 25,431
  • 8
  • 61
  • 93
Mark R
  • 775
  • 1
  • 8
  • 23

0 Answers0