Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 当元素中存在特定值时,如何从列表中删除元素?_R_List - Fatal编程技术网

R 当元素中存在特定值时,如何从列表中删除元素?

R 当元素中存在特定值时,如何从列表中删除元素?,r,list,R,List,我的清单如下: > combination [[1]] [1] 1 10 7 15 [[2]] [1] 1 10 7 15 [[3]] [1] 10 3 10 15 [[4]] [1] 10 3 7 15 [[5]] [1] 10 10 5 15 如何删除同时包含示例7和10的内容 假设我正在删除同时包含7和10的元素,最后应该是这样的: > combination [[3]] [1] 10 3 10 15 [[5]] [1] 10

我的清单如下:

> combination
[[1]]
[1]  1  10  7 15

[[2]]
[1]  1  10  7 15

[[3]]
[1]  10  3  10 15

[[4]]
[1]  10  3  7 15

[[5]]
[1]  10  10  5 15
如何删除同时包含示例7和10的内容

假设我正在删除同时包含7和10的元素,最后应该是这样的:

> combination
[[3]]
[1]  10  3  10 15

[[5]]
[1]  10  10  5 15

感谢您在这方面的帮助。

我们可以使用
Filter

Filter(function(x) !all(c(7, 10) %in% x), combination)

#[[1]]
#[1] 10  3 10 15

#[[2]]
#[1] 10 10  5 15

其他选择可以是:

2) 使用
sapply

combination[sapply(combination, function(x) !all(c(7, 10) %in% x))]
3) 使用
purrr::discard

purrr::discard(combination, ~all(c(7, 10) %in% .x))
4) 使用
purrr::keep

purrr::keep(combination, ~!all(c(7, 10) %in% .x))
数据

combination <- list(c(1, 10, 7, 15), c(1, 10, 7, 15), c(10,  3,  10, 15), 
                    c(10 , 3 , 7, 15), c(10,  10,  5, 15))

组合我们可以在逻辑向量上使用
求和
来子集

combination[sapply(combination, function(x) sum(c(7, 10) %in% x)) != 2]
#[[1]]
#[1] 10  3 10 15

#[[2]]
#[1] 10 10  5 15
数据
组合如果我不仅要省略包含7和10的元素,还要省略包含3和10的元素,我必须多次输入
过滤器
代码吗?@WeiShung不,您不必多次输入代码。您可以执行
Filter(函数(x)!(所有(c(7,10)%in%x | c(3,10)%in%x)),组合)
combination <- list(c(1, 10, 7, 15), c(1, 10, 7, 15), c(10,  3,  10, 15), 
                    c(10 , 3 , 7, 15), c(10,  10,  5, 15))