user3050590
user3050590

Reputation: 1716

dplyr filter () ignores the rows with the value 0, how to change that

In my case, I need the filter function to consider the rows with value 0, but it ignores that.

So I have this data attached and I want to filter out the values of flowers separately for s1 and s2 names. My table is case1

enter image description here

  s1= filter(case1, Flower,Name=="s1")
  s2= filter(case1, Flower,Name=="s2")

This statement runs well, but it ignores the 0 values. I also want to include the 0 values in my analysis. So, how shall I make it to accept that.

EDIT following the comment

Flower,Name,Pl
10, s1, WI
10, s1, WO
30, s1, EI
0,  s1, EO
0,  s2, EI
0,  s2, EO
0,  s1, WI
0,  s2, WO
0,  s2, WO
3,  s2, WI
50, s2, EI
0,  s1, EO
0,  s1, EI

Expected output:

For s1 all values of s1 something like this(including rows with Flower 0)

Flower, Name, Pl
10, s1, WI
10, s1, WO
30, s1, EI
0,  s1, EO

For s2, all values of s2, something like that

Flower, Name, Pl
0,  s2, WO
0,  s2, WO
3,  s2, WI
50, s2, EI

Upvotes: 1

Views: 1955

Answers (1)

akrun
akrun

Reputation: 886938

You can just do

filter(case1, Name=='s1')
#  Flower Name PI
#1     10   s1 WI
#2     10   s1 WO
#3     30   s1 EI
#4      0   s1 EO
#5      0   s1 WI
#6      0   s1 EO
#7      0   s1 EI

By using 'Flower' inside the filter, the '0' values are coerced to 'FALSE' and all others 'TRUE'.

Upvotes: 1

Related Questions