 It's LinkedIn Learning author Monica Wahee with today's data science makeover. Watch while Monica Wahee demonstrates using the quantile command in R to find percentiles of a continuous variable. Hi everyone! Recently I posted some videos about box plots, but I neglected to show you how to find the percentiles, like without a box plot. The box plot does the quartile percentiles for you, but what if you have a continuous variable and you want to know the other percentiles, like the 95th percentile? That's what I'm going to show you in this video. So I added this code on GitHub to the area where I put the box plot code. You can click on the link in the description to the video to go to GitHub and get the code and the data set. So if you watch the box plot video, you'll know that I took data about Massachusetts hospitals, that is public data, and I made this little data set called MA-Hosp. This has our continuous variable in it that we are going to find percentiles of. Let's run this read.csv code and import the data set into R, and then I'll run the data set so we can look at it. As promised, this is a data set of Massachusetts hospitals. See their names? The hospital name is in the first column. There are only two columns in this data frame. The second column is our continuous variable, which is staffed beds. This is the number of beds the hospital has staffing for, so they can admit patients for inpatient services. I don't know how accurate these data are. They are public reporting data, so they change a lot. I wouldn't place any bets based on anything in these data, but they are good for demonstrating percentiles. Let's go back to our code. Okay, so I said I will demonstrate the quantile command. See this next line? This just runs the quantile command on that staffed beds variable. Let's highlight this code and run it and see what happens. Okay, here's our output in the console. See these default percentiles? We have 0%, which is the minimum value, which I guess is 14. Oh, I forgot to tell you. There was a handful of hospitals from the original data with zero staff beds, which I knew were errors, so I just removed them. So yeah, the smallest hospital in this data set does have 14 bets. And if you know of a Massachusetts hospital and you don't see it in this data set, it probably had zero bets, so I removed it. Okay, so we get 0% as the minimum and we get 100% as the maximum. For us, that was 1,019 bets. That's a pretty big hospital. Then in the middle, we get the 25th, 50th, and 75th percentiles. Exactly the minimum ingredients you need for a box plot. That's great, but what if we want other percentiles? I'll show you a few tricks. Let's pretend that, for some reason, we wanted to know the 5th percentile and the 95th percentile. This is what we do. Here's the code. We start the same way with the quantile command, but then this time we add the probes argument. That stands for probabilities. So we said probes equal to the probabilities we want. So you see that? We have a numeric vector that says 0.05 and 0.95. And that's how we tell R we want those two percentiles. And note that because we added a vector here, we have two closed parentheses. All right, let's run this and see what we get. See the answers in the console? It looks like our 5th percentile is 41 and our 95th percentile is 738. Whoa, that's a pretty big hospital. Okay, what other things can we do with quantile? Here's kind of an obvious thing. We could make a numeric vector for the probabilities. That way we can use that vector in the programming so we don't hard code the probabilities. This is helpful for automation. See what I did here? To mix it up, I made our vector B for the 10th percentile and the 90th percentile. And then in the next line, I call quantile again, only this time I use the vector. Let's run this just for fun. See, now we have our 10th and 90th percentiles in the console. Okay, so let's do another trick. Let's say we wanted to know every 10th percentile, like the 10th, 20th, 30th, and so on. We could use a sequence. See this code? Here I replace the numeric vector for the probabilities with a sequence. The way a sequence vector works is you put three arguments, the minimum of the sequence, the maximum of the sequence, and the steps from the minimum to the maximum of the sequence. So as you can see here, I'm requesting the percentiles in a sequence between 0 and 1, and it goes up by increments of 0.1. So that means I should get 0%, 10%, 20%, 30%, and so on up to 100%. Let's highlight and run. Okay, look at that. A lovely array of percentiles. I guess these are technically deciles, right? Okay, one more demonstration before we finish. What if you want to save the quartiles or deciles or whatever percentiles you are getting out of the quantile command in a vector? Well, that's what we do here. See this? We are just taking our last code and saving it in a vector called PCT IELS, which means percentiles for short. Now why would we do that? Well, we might want to automate something, and we might need to have all these things in a vector. I actually do that a lot with quartiles. I save a vector of quartiles, then use the values in the vector in processing to make a categorical variable that classifies the record by quartile. So it says one, two, three, or four. Let's run this and I'll show you what I mean. So let's say you wanted to make a decile column and say which decile each record in my data frame was in, like those really big hospitals. Those would be in the top decile. The cut point for the top decile is 435.8. That number there is in the tenth position of this 11-member vector. So I could simply refer in processing to the tenth position in this vector to get the cut point for the 90th percentile. That way, if the underlying data frame changes, the automation for classifying deciles will still work. Isn't that nifty? And that is your data science makeover for today. Thank you for watching this video. If you thought it was a good use of your time, then please hit the like button. Also, I would just love it if you would look around my channel and if you like what you see, subscribe. I'm trying to add videos that I think my learners will find useful. So feel free to request a topic. Just leave me a comment. Have a peachy day.