#find the row index values with mock treatments using grep
head(grep("mock", growth_data$Condition))[1] 1 2 3 4 5 6
Oftentimes you may need to filter a data set based on a partial character string that is beyond the scope of comparison operators.
R provides such functions (grep and grepl) that match character patterns in specified vector. While both of these functions find patterns, they return different output types based on those patterns.
In our growth data, we only want to print measurements of our controls (i.e. the mock treatments). Our table has many rows, so we only look at the first few results with head().
#find the row index values with mock treatments using grep
head(grep("mock", growth_data$Condition))[1] 1 2 3 4 5 6
To see how grepl works, let’s first try it on a short vector with our three conditions. Both strains (129E and 13C) contain a 1, the mock treatment does not:
#use grepl to search for conditions that contain a 1, returning FALSE/TRUE statements for each element
#with "TRUE" representing matched patterns
grepl("1", c("mock", "129E", "13C"))[1] FALSE TRUE TRUE
On our dataframe, grepl gives one TRUE or FALSE for each of the 594 rows. Instead of printing them all, we can count the TRUE values with sum(), i.e. the number of plants treated with a strain:
#count the rows where the condition contains a 1
sum(grepl("1", growth_data$Condition))[1] 385
Now lets use this to actually filter our data table for a pattern.
#use grep
filter_for_value <- timecourse[grep("mock", timecourse$Condition),]
#check the first rows of our data
head(filter_for_value)#check the dimensions of the original dataframe
dim(timecourse)[1] 2323 4
#check the dimensions of our new dataframe
dim(filter_for_value)[1] 1156 4
filter_for_value <- timecourse[grepl("moc", timecourse$Condition),]
#check the first rows of our data
head(filter_for_value)#check the dimensions of the original dataframe
dim(timecourse)[1] 2323 4
#check the dimensions of our new dataframe
dim(filter_for_value)[1] 1156 4
#filter data set based on values that do not match the specified pattern (by using the ! symbol)
filter_for_not_a_value <- timecourse[!grepl("mock", timecourse$Condition),]
#view dataframe
head(filter_for_not_a_value)#check the dimensions of the original dataframe
dim(timecourse)[1] 2323 4
#check the dimensions of our new dataframe
dim(filter_for_not_a_value)[1] 1167 4
Other comments:
A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.
As an example, let’s grep the strains that have a number with 3 digits.
If you want to know more, see this cheat sheet here
filter_3digits <- growth_data[grepl("[0-9]{3}", growth_data$Condition),]
#check the structure of our data
head(filter_3digits)#check the dimensions of the original dataframe
dim(growth_data)[1] 594 5
#check the dimensions of our new dataframe
dim(filter_3digits)[1] 210 5
Here, [0-9] searches for every number from 0-9 and with {3} we look for three numbers in a row. 129E matches, but 13C does not, because it only has two numbers.
If we want to search for the end of the line to, for example, add two spaces (and thus create a linebreak), we can:
(.)$ with Regex selected\1your_stringThis prints our session info (used R version, packages, etc) and is very important to store to reproduce our code for others
#|eval: false
sessionInfo()R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26200)
Matrix products: default
locale:
[1] LC_COLLATE=English_Netherlands.utf8 LC_CTYPE=English_Netherlands.utf8
[3] LC_MONETARY=English_Netherlands.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Netherlands.utf8
time zone: Europe/Amsterdam
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] htmlwidgets_1.6.4 compiler_4.4.2 fastmap_1.2.0 cli_3.6.5
[5] tools_4.4.2 htmltools_0.5.8.1 yaml_2.3.11 vctrs_0.6.5
[9] rmarkdown_2.30 knitr_1.50 jsonlite_1.9.0 xfun_0.54
[13] digest_0.6.39 rlang_1.1.6 evaluate_1.0.5
Notice: At the moment, we do not want to clean up the working directory, therefore, in our documentation we put a # in front of it. If you want to run the code just remove the #
#rm(list = ls())