![]() > base gsub("dog|cat|hamster|goat|pig","pet", base) # sub in r - regular expression for alternatives We use the | operator within a regular expression to set this up. In the example below, we want to adjust a pet specific text (dog, cat, etc.) to refer the companion animal as a more generic “pet”. Sometimes what you’re looking for may involve more than one thing. # sub in r - regular expression pattern matchingĪs you can see, it tagged multiple subsets of n’s – far more than the original version of this example in our tutorial on sub. In the example below, we’re going to grab the first sequence of 1 – 3 n’s and replace them with a star (not harming any additional n’s in excess of that amount). You can use regular expressions to look for more advanced patterns. We’re going to take a deeper look at regular expressions in a few sections, so keep reading. > phone gsub("]","",phone)Īs you can see, that phone number got a lot skinnier in a hurry! It will also now fit neatly in a numeric field within a database, which is a much easier way to store and manage this type of information. Here’s an example of this below, where we are going to remove all of the punctuation from a phone number. R’s gsub() function can work with regular expressions. "Diogenes the cynic searched Athens for himself." GSub in R – Regular Expressions > base gsub("an honest man", "himself", base)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |