When you use the Unlist function to convert a list of date types to vectors, you do not get the desired result, as follows:
Datelst <-list (sys.date ())datelst[[1]][1] "2015-08-11"datevector <-unlist (datelst) DATEVECTOR[1] 16658
The same reason for using sapply (X, fun, ..., simplify = TRUE, use. NAMES = TRUE), if X is a date-type vector, fun returns the date, then the result will not be the expected date-type vector, as follows:
> date.vector.string <-C ("20010101")> date.vector <-sapply (date.vector.string, Get.date )> date.vector20010101 11323
which
# Get date From date string. Assumed the format of date string is like "20011230"
#
# Args:
# date.string:date string like "20011230"
#
# Returns:
# date.
Get.date <-Function (date.string) {
# deal with the Error in Strptime (x, format, TZ = "GMT"): input string is too long
if (nchar (date.string)! = 8)
Return (NA)
Date <-try (as. Date (date.string, "%y%m%d"))
Return (date)
}
Sapply result, it seems can be thought to call Lapply first, get the list after the call unlist, trying to get withdate.vector.stringThe result of the same type is the vector.
Given the trouble of converting a date-type list to a vector, the best way to do this is to avoid a list of dates and use vectors as much as possible.
The exception that occurs when you use Unlist to convert a list of date data to vectors