0

I would like to subset my data based on two conditions: if X is blank and if Y is blank.

Subsetting based on 1 condition is:

Blank_X <- Q4[is.na(Q4$X),]

How do I add a second condition to this?

Tushar Lad
  • 490
  • 1
  • 4
  • 17
user14142459
  • 79
  • 1
  • 2
  • 6
  • try ```Blank_X <- subset(Q4,is.na(Q4$X) & is.na(Q4$Y))``` – Tushar Lad Aug 25 '20 at 10:37
  • or `Blank_X <- Q4[is.na(Q4$X) & is.na(Q4$Y),]` – GKi Aug 25 '20 at 10:43
  • .. where the logical AND is denoted as `&` and the logical OR is denoted `|`. Be wary that R also knows `&&` and `||` which are similar but noch the same. There is also `!` for logical NOT and `xor` which is set in text, not as symbol. Find out more by typing `help("&")` in your R console. – Bernhard Aug 25 '20 at 10:47
  • 1
    @TusharLad and GKi : Please put your answers in as an answer, not as a comment so that the OP can accept an answer and the question is marked as answered and accepted. – Bernhard Aug 25 '20 at 10:49

2 Answers2

1

Here is one way with subset

Blank_X <- subset(Q4,is.na(Q4$X) & is.na(Q4$Y))

with filter

Blank_X  <- Q4 %>% filter(X!= NA & Y!=NA)

Tushar Lad
  • 490
  • 1
  • 4
  • 17
0

You can use & (and) to combine multiple conditions.

Blank_X <- Q4[is.na(Q4$X) & is.na(Q4$Y),]
GKi
  • 37,245
  • 2
  • 26
  • 48