首页 > 解决方案 > How can I remove all rows that have NA for a certain variable

问题描述

My dataset ('data') has 1719 cases and 6779 variables. I need to weight the data using variable 'weight', however this is missing for 69 cases.

How can I delete the rows that have NA in the weight column, without deleting variables that have NA in any of the other 6778 columns?

标签: rdataframemissing-dataweighted

解决方案


按包含 NA 的列索引行

data[!is.na(data[,"weight"]),]

使用方括号对数据框进行索引以指定行,然后用逗号分隔列:data[rows, columns]

然后,您可以使用该函数提供一个行向量,is.na并在前面加上感叹号,使其有效地成为一个 is。不是.na。

!is.na(data[,"weight"])

推荐阅读