你可以使用sapply和Reduce来获得一个简单的向量:
Reduce(`c`, sapply(a, function(x) paste0(x, c('_present','_not_present'))))
输出
[1] "a_present" "a_not_present" "b_present" "b_not_present" "c_present" "c_not_present
更新
在意识到我的解决方案可能非常慢之后,我又添加了两个函数并进行了基准测试:
fun1 <- function(y) Reduce(`c`, sapply(y, function(x) paste0(x, c('_prfun1 <- function(y) Reduce(`c`, sapply(y, function(x) paste0(x, c('_present','_not_present'))))
fun2 <- function(y) unlist(purrr::map(y, function(i){paste0(i, c('_present','_not_present'))}))
fun3 <- function(y) as.list(sapply(y, function(i) paste0(i, c('_present','_non_present'))))
fun4 <- function(y) array(sapply(y, function(x) paste0(x, c('_present','_not_present'))))
fun5 <- function(y) as.vector(sapply(y, function(x) paste0(x, c('_present','_not_present'))))
fun6 <- function(y){
audit <- list()
j <- 0
for (i in 1:length(y)){
pre <- paste(y[i], "_present")
nonpre <- paste(y[i], "_non_present")
i <- j+1
audit[[i]]<- pre
j <- i+1
audit[[j]] <- nonpre
}
audit
}
x <- list(sample(letters, 1000, replace = TRUE))
microbenchmark::microbenchmark(fun1(x), fun2(x), fun3(x), fun4(x), fun5(x), fun6(x), times = 10L)
结果
Unit: microseconds
expr min lq mean median uq max neval
fun1(x) 2886.633 3107.855 3673.7590 3191.4295 3965.469 6969.991 10
fun2(x) 225.562 235.774 553.8192 277.9055 407.876 2839.410 10
fun3(x) 237.601 245.103 643.7252 261.1835 407.830 3840.739 10
fun4(x) 222.445 230.426 533.4771 249.6535 261.322 3075.166 10
fun5(x) 219.845 232.682 508.5585 253.7870 298.801 2775.606 10
fun6(x) 1531.715 1597.888 2229.4907 1616.1170 1913.850 7245.782 10
fun2 - fun5 的速度大致相同,fun4 和 fun5 稍快一些。最初的解决方案(出乎意料)很慢。