按条件过滤R中的多列 [英] Filter by conditions for multiple columns in R

查看:68
本文介绍了按条件过滤R中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Plan_Year 小于 1年 YEAR 和CAT列中所有 NA 行。

I need to filter by conditions when Plan_Year is 1 less than YEAR and all rows of NA in CAT column.

我有以下代码:

Table <- Table %>% filter(YEAR == Plan_Year+1)

如何添加/修改上面的代码以获取CAT列中所有带有NA的行?

How can I include/modify the code above to obtain all rows with NA in CAT column?

先谢谢了。

表:

YEAR     CAT      Plan_Year
1998     NA       NA
1998     125-1    1997    
1998     171-2    1997
1998     NA       NA  
.
.
2017     114-1    202


推荐答案

使用 | is.na

Table %>% filter(YEAR == Plan_Year+1|is.na(CAT))
  YEAR   CAT Plan_Year
1 1998  <NA>        NA
2 1998 125-1      1997
3 1998 171-2      1997
4 1998  <NA>        NA

或使用基数R

dt[which(dt$YEAR == (dt$Plan_Year + 1)|is.na(dt$CAT)),]
  YEAR   CAT Plan_Year
1 1998  <NA>        NA
2 1998 125-1      1997
3 1998 171-2      1997
4 1998  <NA>        NA

这篇关于按条件过滤R中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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