Gabriele Midolo
Gabriele Midolo

Reputation: 241

Filter out from a dataset rows where values of two variables are both = 0 in dplyr

I have the following data:

x y z
A 0 0
B 1 0
C 0 2
D 1 1
E 2 0
F 2 3
G 1 3
H 0 0
I 3 3

I want to automatically filter out from this dataset all the rows where 'y' and 'z' assumes 0 values at the same time using dplyr (namely I want to exclude A and H only)

Upvotes: 0

Views: 50

Answers (2)

GGamba
GGamba

Reputation: 13680

Using dplyr:

library(dplyr)

df %>% 
    filter(y != 0 | z != 0)

#   x y z
# 1 B 1 0
# 2 C 0 2
# 3 D 1 1
# 4 E 2 0
# 5 F 2 3
# 6 G 1 3
# 7 I 3 3

Upvotes: 1

Mbr Mbr
Mbr Mbr

Reputation: 734

If your dataset is stored in a data.frame called df

You can with dplyr do this :

filter(df, !y == 0, !z == 0)

which will return :

x y z
B 1 0
C 0 2
D 1 1
E 2 0
F 2 3
G 1 3
I 3 3

Upvotes: 0

Related Questions