*Technically I think these are grawlixes, but this is a family blog anyway.
Ever get so mad when you are working with data that you just want to scrawl out a long expletive in the middle of your coding?
Ok, maybe it’s just me. I’m a mild-mannered guy but sometimes we all just need to let it rip on the keyboard.
Leave it to R to take care of yet another obscure data pain point.
While maybe this isn’t the most pressing concern in data, it’s a great exercise to teach you some basics about working with character strings, sampling from a vector and ultimately writing a function in R.
Check out my series on R for Excel users here.
1. Create a vector of characters from which we want to sample.
This is simple enough. We will create a vector consisting of some of our favorite tabloid-worthy characters. Because these are characters and not numbers, we enclose them in quotation marks.
2. Sample elements and collapse into one vector
Next we will use the sample( ) function to sample a specified number of elements from our vector. Sample( ) is a base function in R and does not require installation of additional packages.
In our example we sample from two characters. If you access the value of the vector you created, you will see that each character still stands alone in its own quotation marks.
To combine these characters, we use the paste( ) function (also a base function) to combine these elements into one vector.
*Note: By default, R will sample each character only once until it has run out of options. To tell R to sample items more than once, add “replace = T” as an argument to your function.
3. Create a function to repeat these actions with additional arguments
(This part is a little more advanced.) If you’re an Excel user, you likely work with functions all the time. VLOOKUP, for example, is a function, with given arguments, that repeats certain actions given the inputs and arguments you provide it.
We are going to write our own function called expletive( ) in R that will repeat the sampling and collapsing of character vectors above. All we have to do is provide the function with how many characters should be in our result.
The basic structure of an R function is below.
In our example, we only have one argument: the number of characters we want in the string (see the bonus sections below for additional arguments).
R will use this function to return a result with our specified number of characters. The function should look very familiar from above except that this time we used a variable (nochars) to tell R how many characters to sample from.
We can then use this function to pass a result to a new vector.
So $#@%^ Helpful
I know this post gave me serenity now. But if the junk data becomes too much for you, just know that R and this function are here to help you through.
Which reminds me, check out my series on R and subscribe for updates.
Complete code below….
Leave a Reply