From the course: R for Data Science: Lunch Break Lessons

Using with()

- [Instructor] Long equations can sometimes be ponderous because you have to keep calling up the name of the data set. To make that simpler and more understandable, you could use a command called with, and let's take a look at how that works. First, I need to pull in some data, so I'm going to use the ChickWeight dataset. I use the data command and then ChickWeight. Now that I've got ChickWeight data, let's set up an equation. I'm going to create a new observation called quantile in the ChickWeight data frame. So to do that, I type in ChickWeight, and then, I'm going to bracket quote, and I'll call it quantile. That's just a random name. Into that, I'll assign a series of values. So that was a lot of typing, and you can see that it kind of goes back and forth, and it's not very clear. If I go ahead and run that, it'll work, and it gives me a new observation called quantile, so a factor with four levels. There's a way to clarify that and make it a little bit simpler, and again, that's using with. So let's use with to simplify that equation. First, I'll type in with, W-I-T-H, and then I need to enclose the entire equation with a parentheses, and I'll put that parentheses at the very end, as well. Now I can go through and cut things out. The first thing I need to do, though, is say I'm going to use the ChickWeight data set with all of the following equations. So I'll type in ChickWeight and then a comma, and then here's the equation that I'm going to simplify. So I can get rid of ChickWeight dollar sign, get rid of that, and get rid of it here, as well. Now you can see that, if I'm using with and I'm using the data set ChickWeight, I'm going to cut the weight observation and break on the weight observation in line five. So that makes it much simpler to look at, assuming you understand how with works. So in summary, with is a way to simplify equations when you're using one repeated data set. The formula that I've entered includes two commands, cut and quantile. We've seen cut in a previous weekly R series. Quantile is a statistical function that produces a breakout of some sample quantiles for the given probabilities. You don't need to understand that. All we're focusing on here is the function of with and how it simplifies the equation.

Contents