基于多列的NA过滤数据帧 [英] filtering data frame based on NA on multiple columns
本文介绍了基于多列的NA过滤数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框,将其称为 df
,并具有以下观察结果:
I have the following data frame lets call it df
, with the following observations:
id type company
1 NA NA
2 NA ADM
3 North Alex
4 South NA
NA North BDA
6 NA CA
我只想保留类型和公司列中不包含NA的记录。
I want to retain only the records which do not have NA in column "type" and "company".
id type company
3 North Alex
NA North BDA
我尝试过:
df_non_na <- df[!is.na(df$company) || !is.na(df$type), ]
但这是行不通的。
预先感谢
推荐答案
我们可以获取以下内容的逻辑索引这两个列都使用&
并对行进行子集。
We can get the logical index for both columns, use &
and subset the rows.
df1[!is.na(df1$type) & !is.na(df1$company),]
# id type company
#3 3 North Alex
#5 NA North BDA
或在逻辑矩阵上使用 rowSums
( is.na(df1 [- 1])
)子集。
Or use rowSums
on the logical matrix (is.na(df1[-1])
) to subset.
df1[!rowSums(is.na(df1[-1])),]
这篇关于基于多列的NA过滤数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文