确定具有NA的行数 [英] Determine the number of rows with NAs

查看:47
本文介绍了确定具有NA的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框架如下:

     col1   col2    col3
 1    23    17      NA
 2    55    NA      NA
 3    24    12      13
 4    34    23      12

I我很想找出带有NA的col2和col3中的行数。

I'm interested in finding the number of rows in col2 and col3 with NAs.

我很惊讶下面的代码只给我4而不是2:

I was surprised that the following code only gave me 4 instead of 2:

numNAs <- rowSums(is.na(all[,2:3]))

请帮助。

推荐答案

DF <- read.table(text="     col1   col2    col3
 1    23    17      NA
 2    55    NA      NA
 3    24    12      13
 4    34    23      12", header=TRUE)

这给出了包含任何 NA 列2或3中的值:

This gives the number of rows that contain any NA values in column 2 or 3:

sum(colSums(is.na(DF[,2:3])) > 0)
[1] 2

这篇关于确定具有NA的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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