9.1 filter()

filter() will filter rows.

If we take the geneexp objet as an example, we can filter only down-regulated genes using the filter() function of {dplyr}.

In case you need to import the data again:

geneexp <- read_csv("DataViz_source_files-main/files/expression_20genes.csv")
filter(geneexp, DE=="Down")
## # A tibble: 5 × 4
##   Gene   DE    sample1 sample2
##   <chr>  <chr>   <dbl>   <dbl>
## 1 AKT3   Down     15.1    1.57
## 2 STAT3  Down     15.2    5.46
## 3 HPK1   Down     14.1    7.34
## 4 STAT5  Down     18.6    9.21
## 5 ADAM17 Down     16.1   10.3

== is a logical operator that represents equality. It means that filter will return rows in geneexp that are exactly equal to “Down”.

Logical operators:

Operator Description
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
!x not x
x | y x OR y
x & y x AND y

We can have several conditions.

For example, we may want to extract only rows that have either Up OR Down in DE:

filter(geneexp, DE=="Down" | DE=="Up")
## # A tibble: 8 × 4
##   Gene   DE    sample1 sample2
##   <chr>  <chr>   <dbl>   <dbl>
## 1 AKT3   Down  15.1       1.57
## 2 STAT3  Down  15.2       5.46
## 3 HPK1   Down  14.1       7.34
## 4 TLR8   Up     2.69     16.3 
## 5 STAT5  Down  18.6       9.21
## 6 ADAM17 Down  16.1      10.3 
## 7 PTEN   Up     0.0210   11.2 
## 8 MAPK2  Up     0.998     9.56

Here, we introduce another operator, |, which mean OR, so rows will be kept if there is either Down OR Up in DE column.

A good practice is to assign the filtered output to a new object, for example:

geneexp_filt <- filter(geneexp, DE=="Down" | DE=="Up")