将lapply语法更改为嵌套的r循环



我想请求帮助在我的R函数中改写语法。

我有以下嵌套列表:

x <- list(one = list(one_1 = list(seq = c(rep(1,5), rep(2,4)), start = -1, end = 5), 
one_2 = list(seq = c(rep(2,5), rep(1,5), rep(3,4), rep(2,11)), start = 2, end = 6), one_3 = list(
seq = c(rep(3,4), rep(1,12), rep(4,6)), start = -3, end = 7)), two = list(two_1 = list(
seq = c(rep(1,7), rep(2,4), rep(1,3)), start = 8, end = 222), two_2 = list(seq = c(rep(4,4), rep(1,3), rep(2,6)), 
start = -1, end = 54)))

我对这个列表应用以下函数:

first <- function(input_list, value){
filtered_input <- foreach::foreach(i = seq_along(input_list)) %dopar% {
filtered_output <-  Filter(function(x) any(with(rle(x$seq), lengths[values==value]>=4)) & x$start>=0, input_list[[i]])
lapply(filtered_output, function(x) x)
}
}

和下面的命令:

y <- first(input_list = x, value = 2)

产生如下输出:

desired_y <- list(list(one_2 = list(seq = c(2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 
3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), start = 2, end = 6)), 
list(two_1 = list(seq = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 
1, 1, 1), start = 8, end = 222)))

函数工作正常。但是我想改变它的语法,使用嵌套的for循环。下面是我的尝试,但没有成功:

second <- function(input_list, value){

for (x in seq_along(input_list)){
for (y in seq_along(input_list[[x]])){
filtered_input <- c(filtered_input, foreach::foreach(y) %dopar% filtered_output <- Filter(function(z) any(with(rle(z$seq), lengths[values==value]>=4)) & z$start>=0, input_list[[x]]))
}
}
return(filtered_input)
}
y <- second(input_list = x, value = 2)

这是重写的second。两个输出为identical

library(parallel)
library(doParallel)
#> Loading required package: foreach
#> Loading required package: iterators
library(foreach)
x <- list(one = list(one_1 = list(seq = c(rep(1,5), rep(2,4)), start = -1, end = 5), 
one_2 = list(seq = c(rep(2,5), rep(1,5), rep(3,4), rep(2,11)), start = 2, end = 6), one_3 = list(
seq = c(rep(3,4), rep(1,12), rep(4,6)), start = -3, end = 7)), two = list(two_1 = list(
seq = c(rep(1,7), rep(2,4), rep(1,3)), start = 8, end = 222), two_2 = list(seq = c(rep(4,4), rep(1,3), rep(2,6)), 
                              start = -1, end = 54)))
first <- function(input_list, value){
filtered_input <- foreach::foreach(i = seq_along(input_list)) %dopar% {
filtered_output <- Filter(function(x) 
any(with(rle(x$seq), lengths[values==value]>=4)) & x$start>=0, input_list[[i]]
)
lapply(filtered_output, function(x) x)
}
}
second <- function(input_list, value){
filtered_input <- vector("list", length(input_list))
for (x in seq_along(input_list)){
for (y in seq_along(input_list[[x]])){
z <- foreach::foreach(y, .combine = c) %dopar% 
Filter(function(z) 
any(with(rle(z$seq), lengths[values==value]>=4)) & z$start>=0, input_list[[x]]
)
}
filtered_input[[x]] <- z
}
return(filtered_input)
}

cl <- makeCluster(4L)
registerDoParallel(cl)
y_first <- first(input_list = x, value = 2)
y_second <- second(input_list = x, value = 2)
identical(y_first, y_second)
#> [1] TRUE
stopCluster(cl)

由reprex包(v2.0.1)创建于2022-07-15

最新更新