Entering edit mode
Ryan C. Thompson
★
7.9k
@ryan-c-thompson-5618
Last seen 4 months ago
Icahn School of Medicine at Mount Sinai…
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