From hpages at fredhutch.org  Fri Dec  2 23:13:59 2016
From: hpages at fredhutch.org (=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=)
Date: Fri, 2 Dec 2016 14:13:59 -0800
Subject: [Rd] unlist strips date class
In-Reply-To: <CAPekMC=Od4ib7QJbpk8iGm9woUB4bzM+5BYq3vLqH_4u9ApXNA@mail.gmail.com>
References: <CAPekMC=Od4ib7QJbpk8iGm9woUB4bzM+5BYq3vLqH_4u9ApXNA@mail.gmail.com>
Message-ID: <84139199-8a1f-28f9-2ab0-fae8cd90976b@fredhutch.org>

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



[Rd] unlist strips date class
=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=