[Bioc-devel] list -> AtomicList coercions

Hervé Pagès hpages at fhcrc.org
Tue Nov 29 19:07:43 CET 2011

Hi Michael,

On 11-11-23 02:15 PM, Michael Lawrence wrote:
> Hi guys,
> So it looks like Herve? added some nice new coercions from list to various
> AtomicList types. These delegate to the constructors. I noticed that the
> constructors perform the coercion, but they also preserve names (on each
> element). This is different from the base R coercions, which drop the
> names.

The base R coercions do different things:

 > x <- c(a=1, b=2)

 > as.integer(x)
[1] 1 2

 > as(x, "integer")
a b
1 2

 > as.list(x)
[1] 1

[1] 2

 > as(x, "list")
[1] 1

[1] 2

> Probably being too picky. Also, these are not pure coercions, but in
> some ways a "promotion" to list from List. So that's fine with me. I think
> we could probably add a coercion from "list" to the base list (not
> compressed or simple). For example,  CharacterList would be an alias for
> the CompressedCharacterList coercion.

Yes, we could do that. That means we make CompressedList the preferred
way to represent List (vs SimpleList), which is fine I guess because
this is what the constructors (e.g. IntegerList()) already do by having
'compress=TRUE' by default.

> A related question though: what about coercions from a generic AtomicList
> to each of the specific types. We do not have those, but they would be
> useful. Would those need to drop their names? Consistency is not the only
> concern: it would be faster not to preserve them.

Looks like you already added them:

 > as(IntegerList(a=1:3, b=2:-1), "CharacterList")
CompressedCharacterList of length 2
[["a"]] 1 2 3
[["b"]] 2 1 0 -1

 > as(IntegerList(a=1:3, b=2:-1), "NumericList")
CompressedNumericList of length 2
[["a"]] 1 2 3
[["b"]] 2 1 0 -1

 > as(IntegerList(a=1:3, b=2:-1, compress=FALSE), "NumericList")
CompressedNumericList of length 2
[["a"]] 1 2 3
[["b"]] 2 1 0 -1

 > as(IntegerList(a=letters[1:2], b="33"), "NumericList")
CompressedNumericList of length 2
[["a"]] <NA> <NA>
[["b"]] 33
Warning message:
In structure(as.integer(x), names = names(x)) : NAs introduced by coercion

We don't have coercions for switching between CompressedList
and SimpleList though:

 > as(IntegerList(a=1:3, b=2:-1), "SimpleList")
Error in as(IntegerList(a = 1:3, b = 2:-1), "SimpleList") :
   no method or default for coercing "CompressedIntegerList" to "SimpleList"

Would they be useful too?

> As an aside, it is currently very slow to construct a long e.g.
> CharacterList, because of the lapply() performing the coercion. There are
> obviously faster ways, especially if we do not care about the names.

Feel free to implement those faster ways. I think we should keep the
names by default though e.g. by adding a use.names arg to the
CharacterList() constructor (and to the other constructors), that is
TRUE by default.


> Michael
> 	[[alternative HTML version deleted]]
> _______________________________________________
> Bioc-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel

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 fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

More information about the Bioc-devel mailing list