将 list1
转换为 list2
以及将 list2
转换为 list1
的最优雅的方法是什么?
list1<- c('a','b','c','d','e','f','g','h','i')
list2<- c('abc','def','ghi')
即:以三组为一组接触元素。
感谢:D
最佳答案
让 list1 <- letters[1:10]
(显示当向量的长度不是 3 的倍数时它是如何工作的)。然后,试试这个:
列表 1 到列表 2
# method 1 (seems to be the fastest so far,
# my suspicions about loop being slower were wrong)
list2 <- sapply(split(list1, (seq_along(list1)-1) %/% 3), paste, collapse = "")
# alternatively as @flodel mentions
list2 <- tapply(list1, (seq_along(list1)-1) %/% 3, paste, collapse = "")
tapply
版本的运行时间与 sapply+split
相似(未显示基准测试)。更进一步,在 this post 中使用 @JoshOBrien 的想法
# method 2
pattern <- "(?<=[[:alnum:]]{3})(?=[[:alnum:]])"
strsplit(paste(list1, collapse=""), pattern, perl=TRUE)[[1]]
# [1] "abc" "def" "ghi" "j"
如果您想将最后一部分连接到最后一部分(此处是
j
到 ghi
),请执行以下操作:pattern <- "(?<=[[:alnum:]]{3})(?=[[:alnum:]]{3})"
strsplit(paste(list1, collapse=""), pattern, perl=TRUE)[[1]]
# [1] "abc" "def" "ghij"
列表 2 到列表 1
unlist(strsplit(list2, ""), use.names=FALSE)
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
这是
method1
、 method2
和 eddi 的基准测试:数据:
list1 <- sample(letters, 1e5, replace=TRUE)
职能:
arun <- function() {
pattern <- "(?<=[[:alnum:]]{3})(?=[[:alnum:]])"
strsplit(paste(list1, collapse=""), pattern, perl=TRUE)[[1]]
}
arun2 <- function() {
unname(sapply(split(list1, (seq_along(list1)-1) %/% 3), paste, collapse = ""))
}
eddi <- function() {
substring(paste(list1, collapse = ""),
seq(1, length(list1), 3),
pmin(seq(3, length(list1)+2, 3), length(list1)))
}
基准测试:
require(microbenchmark)
microbenchmark(t1 <- arun(), t2 <- eddi(), t3 <- arun2(), times=10)
identical(t1, t2) # TRUE
identical(t1, t3) # TRUE
# Unit: milliseconds
# expr min lq median uq max neval
# t1 <- arun() 3352.9867 3400.8627 3512.7037 3585.6499 3635.2182 10
# t2 <- eddi() 3302.0925 3318.4184 3356.2109 3409.9728 3487.7220 10
# t3 <- arun2() 474.9235 494.7407 539.4406 641.2605 907.9072 10
关于r - 连接组中的向量元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16407782/