[英]Convert a list of lists to a character vector
我有一个字符列表列表。 例如:
l <- list(list("A"),list("B"),list("C","D"))
因此,您可以看到一些元素是长度> 1的列表。
我想将此列表列表转换为字符向量,但我希望长度> 1的列表在字符向量中显示为单个元素。
unlist
函数没有实现,而是:
> unlist(l)
[1] "A" "B" "C" "D"
还有什么比:
sapply(l,function(x) paste(unlist(x),collapse=""))
为了得到我想要的结果:
"A" "B" "CD"
您可以跳过取消列表步骤。 你已经发现paste0
需要collapse = TRUE
来将一个向量的顺序元素“绑定”在一起:
> sapply( l, paste0, collapse="")
[1] "A" "B" "CD"
如果你不介意多线方法,这是@ thela建议的变体:
x <- lengths(l) ## Get the lengths of each list
l[x > 1] <- lapply(l[x > 1], paste0, collapse = "") ## Paste only those together
unlist(l, use.names = FALSE) ## Unlist the result
# [1] "A" "B" "CD"
或者,如果您不介意使用软件包,请查看“stringi”软件包,特别是stri_flatten
,如stri_flatten
所建议的那样。
这是一个性能比较:
l <- list(list("A"), list("B"), list("B"), list("B"), list("B"),
list("C","D"), list("E","F", "G", "H"),
as.list(rep(letters,10)), as.list(rep(letters,2)))
l <- unlist(replicate(1000, l, FALSE), recursive = FALSE)
funop <- function() sapply(l,function(x) paste(unlist(x),collapse=""))
fun42 <- function() sapply(l, paste0, collapse="")
funv <- function() vapply(l, paste0, character(1L), collapse = "")
funam <- function() {
x <- lengths(l)
l[x > 1] <- lapply(l[x > 1], paste0, collapse = "")
unlist(l, use.names = FALSE)
}
funj <- function() sapply(l, stri_flatten)
funamj <- function() {
x <- lengths(l)
l[x > 1] <- lapply(l[x > 1], stri_flatten)
unlist(l, use.names = FALSE)
}
library(microbenchmark)
microbenchmark(funop(), fun42(), funv(), funam(), funj(), times = 20)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# funop() 78.21822 84.79588 85.30055 85.36399 86.90540 90.48321 20 e
# fun42() 56.16938 57.35735 61.60008 58.04969 65.82836 81.46482 20 d
# funv() 54.64101 56.23245 60.07896 57.26049 63.96815 78.58043 20 d
# funam() 45.89760 46.89890 48.99810 47.29617 48.28764 56.92544 20 c
# funj() 28.73405 29.94041 32.00676 30.56711 31.11448 39.93765 20 b
# funamj() 18.64829 19.01328 21.05989 19.12468 19.52516 32.87569 20 a
注意:此方法的相对效率取决于length(x) > 1
列表项的数量。 如果他们中的大多数都要> 1
,那么就采用@ 42-的方法。 stri_flatten
只会提高性能,如果您将长字符向量粘贴在一起,就像在上面的基准测试中使用的样本列表一样,否则,它没有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.