[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:
library(stringr)
library(plyr)
flatten.CharacterList <- function(x, sep=",") {
if (is.list(x)) {
x[!is.na(x)] <- laply(x[!is.na(x)], str_c, collapse=sep,
.parallel=TRUE)
x <- as(x, "character")
}
x
}
-Ryan
More information about the Bioconductor
mailing list