[Rd] unlist strips date class
Hervé Pagès
hpages at fredhutch.org
Fri Dec 2 23:13:59 CET 2016
Hi,
On 12/02/2016 10:45 AM, Kenny Bell wrote:
> Is this a bug?
>
>> unlist(list(as.Date("2015-01-01")))
> [1] 16436
Good question.
More generally one might reasonably expect 'unlist(x)' to be equivalent
to 'do.call(c, x)' on a list 'x' where all the list elements are atomic
vectors:
x <- list(1:3, letters[1:2])
unlist(x)
# [1] "1" "2" "3" "a" "b"
do.call(c, x)
# [1] "1" "2" "3" "a" "b"
However, if the list contains dates:
x <- list(as.Date("2015-01-01") + 0:2, as.Date("2016-12-02"))
unlist(x)
# [1] 16436 16437 16438 17137
do.call(c, x)
# [1] "2015-01-01" "2015-01-02" "2015-01-03" "2016-12-02"
then 'unlist(x)' drops the attributes and 'do.call(c, x)' does not.
And if the list contains factors:
x <- list(factor("Z"), factor(c("a", "Z", "b")))
unlist(x)
# [1] Z a Z b
# Levels: Z a b
do.call(c, x)
# [1] 1 1 3 2
then it's the other way around. Also note that the fact that the first
2 numbers in the result of 'do.call(c, x)' are the same is
confusing/misleading. Hard to see how this result could be useful
to anybody.
These inconsistencies might well be documented but hopefully they can
be addressed.
Cheers,
H.
--
Hervé Pagès
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024
E-mail: hpages at fredhutch.org
Phone: (206) 667-5791
Fax: (206) 667-1319
More information about the R-devel
mailing list