0
0
R Programmingprogramming~5 mins

grep and grepl in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the grep() function do in R?

grep() searches for patterns in a character vector and returns the indices of the elements that match the pattern.

Click to reveal answer
beginner
How is grepl() different from grep()?

grepl() returns a logical vector (TRUE or FALSE) indicating if the pattern was found in each element, while grep() returns the indices of matching elements.

Click to reveal answer
beginner
What will grep("cat", c("cat", "dog", "caterpillar")) return?

It will return 1 3 because "cat" is found in the 1st and 3rd elements.

Click to reveal answer
beginner
What does grepl("dog", c("cat", "dog", "caterpillar")) return?

It returns FALSE TRUE FALSE showing which elements contain "dog".

Click to reveal answer
intermediate
Can grep() and grepl() use regular expressions?

Yes, both functions support regular expressions to find complex patterns in text.

Click to reveal answer
What does grep() return when it finds matches?
AThe matching text itself
BLogical vector of TRUE/FALSE
CNumber of matches
DIndices of matching elements
Which function returns a logical vector indicating matches?
Agrepl()
Bgrep()
Cgsub()
Dsub()
What will grepl("a", c("apple", "banana", "cherry")) return?
A[TRUE, TRUE, FALSE]
B[FALSE, TRUE, TRUE]
C[TRUE, FALSE, TRUE]
D[FALSE, FALSE, FALSE]
If you want to get the actual matching text, which function should you use?
Agrep()
Bgrepl()
Cregmatches() with regexpr()
Dgrepl() with invert = TRUE
Can grep() and grepl() handle case-insensitive searches?
ANo, they are case-sensitive only
BYes, by setting <code>ignore.case = TRUE</code>
COnly <code>grep()</code> can
DOnly <code>grepl()</code> can
Explain the difference between grep() and grepl() in R.
Think about what each function returns when you search text.
You got /4 concepts.
    How can you use grep() or grepl() to find if a word exists in a list of strings?
    Consider what output you want: positions or logical answers.
    You got /4 concepts.