CL Chan
CL Chan

Reputation: 1

How to choose a observation with certain variable are all FALSE?

  data.frame = completed  

  unique_ID  Var1  Var2  Var3  
  1000820-1  FALSE FALSE FALSE  
  1000820-2  FALSE FALSE  TRUE  
  1000823-1  FALSE  TRUE FALSE  
  1000823-2  FALSE FALSE  TRUE  
  1000823-3  FALSE FALSE  TRUE  
  1000825-49  TRUE FALSE FALSE  
  1000830-1  FALSE  TRUE FALSE  
  1000830-2  FALSE FALSE  TRUE  

How do I choose those uniqueID with all variables are FALSE ?

I.e output:

unique_ID   Var1  Var2  Var3  
1000820-1  FALSE FALSE FALSE  
1000825-49 FALSE FALSE FALSE  

Upvotes: 0

Views: 37

Answers (1)

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

One Solution would be this


df[rowSums(df[2:4]) == 0,]

unique_ID  Var1  Var2  Var3
1 1000820-1 FALSE FALSE FALSE

TRUEand FALSE are counted as 1and 0, so rowSums gives you the number of TRUE.

Alternative with dplyr-package:

library(dplyr)

df %>% filter_all(all_vars(. != TRUE))

unique_ID  Var1  Var2  Var3
1 1000820-1 FALSE FALSE FALSE

Upvotes: 2

Related Questions