These are ways to search text without writing the explicitly what you are searching.
This allows you to search for multiple texts at the same time, or to search for something without knowing how it is written exactly
Patterns (what to search)
any digit: "\\d"
any word character (letters, _, digits): "\\w"
any character (including special symbols and space): "."
If you want to create your set of characters allowed in the pattern, just use []
I want to search only uppercase letters: "[A-Z]"
Special symbols: "[\\.\\*\\-]" (put two backslashes \\ before the symbol)
Quantifiers (how many times the pattern occurs)
Specific number of times: A{X}, where X is the number of times A appears
Between X and Y times: e{X,Y}, e appears at least X times, at most Y times (in tandem)
At least X times, but can be more: T{X,} (nothing after the “,” means INFINITY)
At least once, but can be more: N+
May not appear, may appear multiple times: C*
Anchors
Text MUST start with the pattern “ATG”: "^ATG" (start pattern with ^)
Transcript must end with stop codon: "T[GA][GA]$" (end pattern with $)
Split strings (text)
Use symbol or letter to split a text into a vector (similar to excel’s “text to columns” command)
Example: arabidopsis genes are named as “AT” chromosome number “G” gene number. So we can separate the gene number from the chromosome number using “G” as the pattern/split.
split is the symbol/letter that will be used to separate the text
The result is a list. If x has 10 elements, the resulting list will have also 10 elements, each one a character vector.
Arabidopsis_Genes =c("AT1G01020", "AT1G01030","AT1G01040")# With strsplit and str_split, in each list, the chromosome number is the first element, the gene number is the secondstrsplit(x = Arabidopsis_Genes, split ='G')
str_split_1 - string has to be a character vector of size 1
Arabidopsis_Genes =c("AT1G01020", "AT1G01030","AT1G01040")# str_split_1 accepts only one element at a time# The result is the same as str_split(string = Arabidopsis_Genes, pattern = "G")[[1]]str_split_1(string = Arabidopsis_Genes[1], pattern ="G")
[1] "AT1" "01020"
str_split_fixed(string, pattern, n) - similar to str_split, but the result is a matrix (vector with columns and rows). You need to specify n, the max number of pieces to return for each string from the input
Arabidopsis_Genes =c("AT1G01020", "AT1G01030","AT1G01040")# str_split_fixed returns a matrix with nrow == length(string) and ncol == nstr_split_fixed(string = Arabidopsis_Genes, pattern ="G", n =2)
str_split_i(string, pattern, i) - similar to str_split_fixed, but i is used to specify which column we want to keep from the matrix.
Arabidopsis_Genes =c("AT1G01020", "AT1G01030","AT1G01040")# With str_split_i, you specify which part of the string you want to keep after the split# If we want to keep just the gene number in this case, we select the second part, the one that comes after "G"str_split_i(string = Arabidopsis_Genes, pattern ="G", i =2)
[1] "01020" "01030" "01040"
Search for word or pattern: grep and grepl
Both are similar to “CTRL+F”, but have some extra capabilities.
x is the character vector or even data.frame in which you are searching
If fixed is omitted or F, search will allow regex, else it will take patterns as literal words (meaning \w will match only \w, not “any letter, number or _“)
grep result is either a vector with the index of the match: