R 在列表中追加两个连续项
如何使用另一个列表长度在R列表中追加两个连续项R 在列表中追加两个连续项,r,list,append,R,List,Append,如何使用另一个列表长度在R列表中追加两个连续项 a您可以使用purrr::map从a中创建一个元素的元组,并与\u present'&'u not\u present'连接,然后只需取消列出'结果以获得一个向量:) 您可以使用sapply。这里我使用了paste0而不是paste as.list(sapply(a, function(i) paste0(i, c('_present','_non_present')))) [[1]] [1] "a_present" [[2
a您可以使用purrr::map
从a中创建一个元素的元组,并与\u present'&'u not\u present'连接,然后只需
取消列出'结果以获得一个向量:)
您可以使用sapply
。这里我使用了paste0
而不是paste
as.list(sapply(a, function(i) paste0(i, c('_present','_non_present'))))
[[1]]
[1] "a_present"
[[2]]
[1] "a_non_present"
[[3]]
[1] "b_present"
[[4]]
[1] "b_non_present"
[[5]]
[1] "c_present"
[[6]]
[1] "c_non_present"
您可以使用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
更新
在意识到我的solulion可能非常慢之后,我又添加了两个函数并进行了基准测试:
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)
fun2
-fun5
的速度大致相同,fun4
和fun5
稍快一些。原始解决方案(出乎意料地)慢。您可以使用外部解决方案:
outer(a, c('_present', '_non_present'), paste0)
# [,1] [,2]
#[1,] "a_present" "a_non_present"
#[2,] "b_present" "b_non_present"
#[3,] "c_present" "c_non_present"
如果希望以列表形式输出:
as.list(t(outer(a, c('_present', '_non_present'), paste0)))
#[[1]]
#[1] "a_present"
#[[2]]
#[1] "a_non_present"
#[[3]]
#[1] "b_present"
#[[4]]
#[1] "b_non_present"
#[[5]]
#[1] "c_present"
#[[6]]
#[1] "c_non_present"
干得好。我假设OP想要一个类似于他们的audit
的列表作为输出。然而,Reduce
的速度很慢这一事实非常有趣。@Edward R中的函数调用在计算上可能会很昂贵,特别是在创建新对象时(我从经验中知道这一点)。这就是为什么我认为Reduce
解决方案会很慢。
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
outer(a, c('_present', '_non_present'), paste0)
# [,1] [,2]
#[1,] "a_present" "a_non_present"
#[2,] "b_present" "b_non_present"
#[3,] "c_present" "c_non_present"
as.list(t(outer(a, c('_present', '_non_present'), paste0)))
#[[1]]
#[1] "a_present"
#[[2]]
#[1] "a_non_present"
#[[3]]
#[1] "b_present"
#[[4]]
#[1] "b_non_present"
#[[5]]
#[1] "c_present"
#[[6]]
#[1] "c_non_present"