The exception that occurs when you use Unlist to convert a list of date data to vectors

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.