Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in R Programming by (5.3k points)

When I unlist a list of dates it turns them back into numerics. Is that normal? Any workaround other than re-applying as.Date?

> dd <- as.Date(c("2013-01-01", "2013-02-01", "2013-03-01"))

> class(dd)

[1] "Date"

> unlist(dd)

[1] "2013-01-01" "2013-02-01" "2013-03-01"

> list(dd)

[[1]]

[1] "2013-01-01" "2013-02-01" "2013-03-01"

> unlist(list(dd))

[1] 15706 15737 15765

Is this a bug?

1 Answer

0 votes
by

According to R Documentation:

Where possible the list elements are coerced to a common mode during the unlisting, and so the result often ends up as a character vector. Vectors will be coerced to the highest type of the components in the hierarchy NULL < raw < logical < integer < real < complex < character < list < expression: pairlists are treated as lists

Instead of using the unlist function, you can use the do.call() function as follows:

dd <- as.Date(c("2013-01-01", "2013-02-01", "2013-03-01"))

dd <- list(dd, dd)

d <- do.call("c", dd)

> d

[1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-01-01" "2013-02-01" "2013-03-01"

> class(d)

[1] "Date"

Browse Categories

...