The Best %^&@! Expletive Generator in R*

July 7, 2018
By

(This article was first published on George J. Mount, and kindly contributed to R-bloggers)

*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.

And let me tell you, holding down the shift key as I peck away at the upper row of my keyboard is just not satisfying.

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….

To leave a comment for the author, please follow the link and comment on their blog: George J. Mount.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)