如何在 lapply 列表中使用条件 lapply、Filter、Between 在 R 中进行过滤?
How to filter in lapply list with conditionals lapply, Filter, Between in R?
我有这个:
res.list<-list(c("X2", "X3", "X4"), c("X2", "X3", "X4"), c("X1", "X2", "X3"))
result<- lapply(res.list, function(x) {
unlist(lapply(seq_along(x), function(y) combn(x, y, list)), recursive = FALSE)
})
result<-lapply(result, Filter,f = function(x){
ifelse(
seq_along(result)==1,
between(length(x),1,3),
between(length(x),2,2)
) %>%.[!is.na(.)]})
1- 我有一个列表“res.list”
2- 合并第 1 至“n”列而不重复。
3-按条件筛选结果,删除元素为NULL
预期输出:“结果”
[[1]]
[[1]][[1]][1] "X2"
[[1]][[2]][1] "X3"
[[1]][[3]][1] "X4"
[[1]][[4]][1] "X2" "X3"
[[1]][[5]][1] "X2" "X4"
[[1]][[6]][1] "X3" "X4"
[[1]][[7]][1] "X2" "X3" "X4"
[[2]]
[[2]][[4]][1] "X2" "X3"
[[2]][[5]][1] "X2" "X4"
[[2]][[6]][1] "X3" "X4"
[[3]]
[[3]][[4]][1] "X2" "X3"
[[3]][[5]][1] "X2" "X4"
[[3]][[6]][1] "X3" "X4"
代码未return结果预期输出,如果您有任何改进想法并使其更通用,那将很有启发性。
此致
我们可能需要
result[-1] <- lapply(result[-1], \(x) x[between(lengths(x), 1, 2)])
我有这个:
res.list<-list(c("X2", "X3", "X4"), c("X2", "X3", "X4"), c("X1", "X2", "X3"))
result<- lapply(res.list, function(x) {
unlist(lapply(seq_along(x), function(y) combn(x, y, list)), recursive = FALSE)
})
result<-lapply(result, Filter,f = function(x){
ifelse(
seq_along(result)==1,
between(length(x),1,3),
between(length(x),2,2)
) %>%.[!is.na(.)]})
1- 我有一个列表“res.list”
2- 合并第 1 至“n”列而不重复。
3-按条件筛选结果,删除元素为NULL
预期输出:“结果”
[[1]]
[[1]][[1]][1] "X2"
[[1]][[2]][1] "X3"
[[1]][[3]][1] "X4"
[[1]][[4]][1] "X2" "X3"
[[1]][[5]][1] "X2" "X4"
[[1]][[6]][1] "X3" "X4"
[[1]][[7]][1] "X2" "X3" "X4"
[[2]]
[[2]][[4]][1] "X2" "X3"
[[2]][[5]][1] "X2" "X4"
[[2]][[6]][1] "X3" "X4"
[[3]]
[[3]][[4]][1] "X2" "X3"
[[3]][[5]][1] "X2" "X4"
[[3]][[6]][1] "X3" "X4"
代码未return结果预期输出,如果您有任何改进想法并使其更通用,那将很有启发性。
此致
我们可能需要
result[-1] <- lapply(result[-1], \(x) x[between(lengths(x), 1, 2)])