The five minutes you spend each week will provide you with a building block you can use in the next two hours at work. Review language basics, discover methods to improve existing R code, explore new and interesting features, and learn about useful development tools and libraries that will make your time programming with R that much more productive.
All series code samples can be downloaded at https://github.com/mnr/five-minutes-of-R.
Skill Level Intermediate
- [Instructor] If you've used the Tidyverse or seen code from the Tidyverse, you've seen the pipeline operator. That's the percent greater than percent symbol. There are other uses for that package, it's called magrittr. And it contains several operators that you'll find useful. Let's take a look at it. First you install the package, magrittr, and bring it into the library. And we can start with the very simple example of what this looks like. Without the pipeline you do something like this, where you'd create a variable.
We'll assign fivenum. Fivenum generates a set of quartiles, one to 23, and you can see now we have a myvar with five numbers in it. And then I'd like to plot that value, so I'll use plot, and I'll put in myvar, and I'm going to plot it against one to five. So there's a plot for us. I can make this simpler by getting rid of the myvar and replacing the right hand side with a pipeline symbol, and then placing plot at the end, getting rid of myvar, and I can even make it simpler if I get rid of this and put this data set to the left of fivenum, and then pipe that into fivenum.
There it is. So now when I run this you'll see I also get a plot, it's the same thing, but it's a little bit cleaner. Now inside the magrittr package is something called T. And it looks like this. If I take the same code that I just created, and let's copy it down here into T, I'll get rid of this plot so you can see it show up again. Now what I haven't seen is the actual numbers that fivenum is generating. And I can see that if I put in a T right there, which creates a T for the fivenum pipeline, and I'll hit command return and two things happen.
You see that there's a plot generated just like before, but you'll also see that down here we have the five numbers that were generated by fivenum, and the T, put those numbers out to the consoles so we can see what the actual values were. There is another operator inside the magrittr package, it's called the compound assignment operator. This is a little bit unusual. All I want is one through 23 into myvar, and if you look at myvar up here in the environments, you'll see that there's an int.
It's 23 characters long. Now what I can do is use myvar, and then I'll use the compound assignment operator, which is percent, less than greater than percent, and fivenum. Now watch what happens when I run this. You'll notice in the environments right now, it's int one through 23, and I run this command return, and myvar now becomes numbers one through five.
What's happened is the value from myvar was piped into fivenum. Fivenum did it's calculation and then piped it back to myvar. It essentially bounced the data through the pipeline and then back out again. So it's a way to do a quick calculation and save the result. Finally there's the dot operator. And to understand this what you need to know is that the numbers come through, but the recipient of those numbers may not necessarily know what to do with them.
So let's set up here a pipeline, and we'll send one through 23 through fivenum. And then we'll pipe that to the letters built in data set. So what I'll do is take one through 23, pipe it through fivenum, and then assign letters to the numbers that result. So let's run that, and you'll see that I get an error message, and that's because letters really doesn't know how to use the numbers that are coming from fivenums.
So I can do is use the dot operator. I'll say put the number that you get right there. Now when I run this you'll see that the number flows through fivenum and into letters and then letters uses it in this location. So that's the magrittr package, there are several operators inside it that you may not have seen before, and you'll find them to be useful in certain cases.