相关疑难解决方法(0)

dplyr过滤器,具有多列条件

这是一个虚拟数据:

father<- c(1, 1, 1, 1, 1)
mother<- c(1, 1, 1, NA, NA) 
children <- c(NA, NA, 2, 5, 2) 
cousins   <- c(NA, 5, 1, 1, 4) 


dataset <- data.frame(father, mother, children, cousins)  
dataset


father  mother  children cousins
1      1       NA      NA
1      1       NA       5
1      1        2       1
1     NA        5       1
1     NA        2       4
Run Code Online (Sandbox Code Playgroud)

我想过滤这一行:

  father  mother  children cousins
    1      1       NA      NA
Run Code Online (Sandbox Code Playgroud)

我可以这样做:

test <- dataset %>% 
filter(father==1 & mother==1) %>%
filter (is.na(children)) %>%
filter …
Run Code Online (Sandbox Code Playgroud)

r dplyr

13
推荐指数
4
解决办法
3万
查看次数

在多列上过滤基于NA的数据帧

我有以下数据框让我们称之为df

id   type   company
1    NA      NA
2    NA      ADM
3    North   Alex
4    South   NA
NA   North   BDA
6    NA      CA
Run Code Online (Sandbox Code Playgroud)

我想只保留"类型"和"公司"栏中没有NA的记录

id   type   company
3    North   Alex
NA   North   BDA
Run Code Online (Sandbox Code Playgroud)

我累了

 df_non_na <- df[!is.na(df$company) || !is.na(df$type), ]
Run Code Online (Sandbox Code Playgroud)

但这没效果.

提前致谢

r dataframe

7
推荐指数
5
解决办法
1万
查看次数

标签 统计

r ×2

dataframe ×1

dplyr ×1