[BioC] Easy way to convert CharacterList to character, collapsing each element?

Ryan C. Thompson rct at thompsonclan.org
Tue Dec 17 00:04:27 CET 2013

Hi all,

I have some annotation data in a DataFrame, and of course since 
annotations are not one-to-one, some of the columns are CharacterList or 
similar classes. I would like to know if there is an efficient way to 
collapse a CharacterList to a character vector of the same length, such 
that for elements of length > 1, those elements are collapsed with a 
given separator. The following is what I came up with, but it is very 
slow for large CharacterLists:

flatten.CharacterList <- function(x, sep=",") {
   if (is.list(x)) {
     x[!is.na(x)] <- laply(x[!is.na(x)], str_c, collapse=sep, 
     x <- as(x, "character")


More information about the Bioconductor mailing list