0
0
R Programmingprogramming~10 mins

grep and grepl in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the indices of elements containing the letter 'a'.

R Programming
words <- c("apple", "banana", "cherry", "date")
indices <- grep([1], words)
print(indices)
Drag options to blanks, or click blank then click option'
A"c"
B"b"
C"a"
D"d"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the pattern inside quotes.
Using the wrong letter in the pattern.
2fill in blank
medium

Complete the code to check which elements contain the letter 'e' and return TRUE or FALSE.

R Programming
words <- c("apple", "banana", "cherry", "date")
contains_e <- grepl([1], words)
print(contains_e)
Drag options to blanks, or click blank then click option'
A"e"
B"o"
C"a"
D"i"
Attempts:
3 left
💡 Hint
Common Mistakes
Using grep instead of grepl when expecting TRUE/FALSE.
Using a letter not present in any word.
3fill in blank
hard

Fix the error in the code to correctly find words containing 'n'.

R Programming
words <- c("apple", "banana", "cherry", "date")
result <- grep([1], words, value = TRUE)
print(result)
Drag options to blanks, or click blank then click option'
An
B"n"
C'n'
Dn"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the pattern.
Using mismatched or incomplete quotes.
4fill in blank
hard

Fill both blanks to create a named logical vector showing which words contain 'a'.

R Programming
words <- c("apple", "banana", "cherry", "date")
result <- setNames(grepl([1], words), [2])
print(result)
Drag options to blanks, or click blank then click option'
A"a"
Bwords
C"words"
Dletters
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the second blank, which should be a variable.
Using the wrong pattern string.
5fill in blank
hard

Fill all three blanks to create a named vector of words containing 'e' with their indices.

R Programming
words <- c("apple", "banana", "cherry", "date")
indices <- grep([1], words)
selected_words <- words[[2]]
named_vector <- setNames(selected_words, [3])
print(named_vector)
Drag options to blanks, or click blank then click option'
A"e"
Bindices
D"words"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around indices in the second or third blank.
Using the wrong pattern string.