r - 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?
解决方案
按包含 NA 的列索引行
data[!is.na(data[,"weight"]),]
使用方括号对数据框进行索引以指定行,然后用逗号分隔列:data[rows, columns]
然后,您可以使用该函数提供一个行向量,is.na
并在前面加上感叹号,使其有效地成为一个 is。不是.na。
!is.na(data[,"weight"])
推荐阅读
- haskell - 在方法上获取非详尽的模式异常
- excel - 如何修复 - Excel VBA 复制和粘贴值 - ThisWorkbook 错误
- xslt - XSLT:使用表中的搜索结果创建新主题
- scala - 重命名 SPARK 中具有相同名称的字段
- python - 无限期循环列表
- walmart-api - Walmart API 因某些搜索而损坏
- visual-studio - 无法设置执行策略以允许我在 Visual Studio 生成后事件中执行脚本
- javascript - HTML Javascript 使用 Slider bar 步骤更改图像
- javascript - 参考错误:PDFViewer 未定义
- node.js - 如何获取用户执行脚本的目录名/路径?