Replacing matches in strings
1. Replacing matches in strings
Our final stringr function for the chapter is str_replace. str_replace replaces a pattern in the input strings with a specified replacement string.2. str_replace()
Let's take our friends Tom & Jerry and replace the ampersand with the string and. str_replace only replaces the first occurrence of the pattern, so with the chipmunks we only replace one ampersand. Never fear, there's a variant called str_replace_all that replaces every occurrence of the pattern. Of course you can pass in a vector of strings3. str_replace() with vectors
and you'll get out a vector of strings with the replacements made, much like "Find & Replace" in your favorite text editor. You'll try out str_replace in the next exercise. Then, we'll give you a challenge where you'll have to remember all the stringr functions you've learned so far, to answer some questions about DNA strings. We'll wrap up the chapter4. Let's practice!
with you exploring how stringr functions can be combined to perform quite complicated manipulations.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.