如何从矩阵中删除所有带有NA的行? [英] How to remove all rows with NA from a matrix?

查看:639
本文介绍了如何从矩阵中删除所有带有NA的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 :一个矩阵 y ,它有两列(行数不同,取决于输入参数).第一列中的所有元素都是整数.

I have: A matrix y and it has two columns (the number of rows is different and depends on the input parameters). All the elements in the first columns are integers.

我需要 :如果第二列的元素为NA,则需要为每一行删除该行.

I need: for each row if the element of the second column is NA, I need to remove this row.

我该怎么办?我唯一的想法是创建另一个矩阵,如果第一个矩阵中的一行没有NA,则将其添加到第二个矩阵中.但是我敢打赌,R中有一种更优雅的方法.

How can I do it? My only idea is to create another matrix and if a row from my first matrix doesn't have NA, add it to the second matrix. But I bet there's a more elegant way of doing so in R.

谢谢!

推荐答案

包含一些组成数据的示例:

An example with some made up data:

m <- matrix(c(1:3,c(1,NA,4)),ncol=2)
m[!is.na(m[,2]),]

这篇关于如何从矩阵中删除所有带有NA的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆