R:如果行中的元素满足某些特征,如何删除行?
我试图找出一种方法,如果该行中的单元格满足特定特征,则删除矩阵行。例如:
> mm <- matrix(c(1,2,3,2,3,4,1,2,3,4),5,2)
> mm
[,1] [,2]
[1,] 1 4
[2,] 2 1
[3,] 3 2
[4,] 2 3
[5,] 3 4
如果该行中的第 1 列元素为 2,我想删除该行。最后我想要这样:
[,1] [,2]
[1,] 1 4
[2,] 3 2
[3,] 3 4
我该怎么做?
如果我需要删除第一个列元素对应于列表中包含的一组数字的行,而不是删除第一个列元素为 2 的所有行,那么更通用的方法怎么样?例如,
delete_list <- c(2,3)
执行此操作的最佳方法是什么?
先感谢您。
I am trying to figure out a way to delete rows of matrix if a cell in that row satisfies a certain characteristic. For example:
> mm <- matrix(c(1,2,3,2,3,4,1,2,3,4),5,2)
> mm
[,1] [,2]
[1,] 1 4
[2,] 2 1
[3,] 3 2
[4,] 2 3
[5,] 3 4
I want to delete rows if the 1st column element in that row is 2. At the end I want this:
[,1] [,2]
[1,] 1 4
[2,] 3 2
[3,] 3 4
How could I do this?
And what about a more general method if instead of deleting all rows who's first column element is 2, I needed to delete rows who's first column element corresponds to a set of numbers that are contained in a list? For example
delete_list <- c(2,3)
What is the best way to do this?
Thank You in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用
This 即可,因为
返回
并且本质上您正在使用此布尔数组来选择要选择的行。
Just use
This works because
returns
and essentially you are using this boolean array to choose which rows to pick.
未经测试...
基本上就是我认为你所追求的。
编辑:该死,忍者一分钟!
Not tested...
is basically what I think you're after.
Edit: damn, ninja'd by one minute!