0
0
R Programmingprogramming~10 mins

sub and gsub 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 replace the first occurrence of 'cat' with 'dog' in the string.

R Programming
result <- sub([1], "dog", "cat and cat")
Drag options to blanks, or click blank then click option'
A"cat"
B"dog"
C"and"
D"cat and cat"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the replacement string as the pattern.
Using the whole string as the pattern.
2fill in blank
medium

Complete the code to replace all occurrences of 'cat' with 'dog' in the string.

R Programming
result <- gsub([1], "dog", "cat and cat")
Drag options to blanks, or click blank then click option'
A"cat"
B"dog"
C"and"
D"cat and cat"
Attempts:
3 left
💡 Hint
Common Mistakes
Using sub instead of gsub for all replacements.
Using the replacement string as the pattern.
3fill in blank
hard

Fix the error in the code to replace the first 'a' with 'o' in the string.

R Programming
result <- sub([1], "o", "banana")
Drag options to blanks, or click blank then click option'
Aa
B"a"
C'a'
Dbanana
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted letters as patterns.
Using the whole string as the pattern.
4fill in blank
hard

Fill both blanks to replace all digits with '#' in the string.

R Programming
result <- gsub([1], [2], "Phone: 123-456-7890")
Drag options to blanks, or click blank then click option'
A"[0-9]"
B"#"
C"-"
D"Phone"
Attempts:
3 left
💡 Hint
Common Mistakes
Using sub instead of gsub to replace all digits.
Using wrong pattern that doesn't match digits.
5fill in blank
hard

Fill all three blanks to replace all vowels with '*' in the string.

R Programming
result <- gsub([1], [2], [3])
Drag options to blanks, or click blank then click option'
A"[aeiouAEIOU]"
B"*"
C"Hello World"
D"[0-9]"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pattern that doesn't match vowels.
Using sub instead of gsub to replace all vowels.