Remove the rows of data frame whose cells match a given vector

 ̄綄美尐妖づ 提交于 2019-12-03 20:46:30

apply your test to each row:

keeps <- apply(ff, 1, function(x) !any(x %in% 8:10))

which gives a boolean vector. Then subset with it:

ff[keeps,]

   j.1 j.2 j.3
1    1   2   3
2    2   3   4
3    3   4   5
4    4   5   6
5    5   6   7
11  11  12  13
12  12  13  14
13  13  14  15
> 

I suppose the apply strategy may turn out to be the most economical but one could also do either of these:

 ff[ !rowSums( sapply( ff, function(x) x %in% 8:10) ) , ]
ff[ !Reduce("+", lapply( ff, function(x) x %in% 8:10) ) , ]

Vector addition of logical vectors, (equivalent to any) followed by negation. I suspect the first one would be faster.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!