R tutorial: Subsets and histograms

Learn how to plot histograms of baseball pitch data in R, create binary variables to identify pitch types like sliders, and use subset() to isolate specific pitches. The tutorial shows how a multimodal histogram reveals distinct groups, then narrows to individual pitch types for clearer distributions.
Chapters
Now that you've prepared the dates appropriately, it's time to start exploring your data. You'll begin by exploring the start speed variable. This variable indicates the velocity of each pitch thrown as it leaves the pitcher's hand. It's important to note that the velocity measurements are in miles per hour and the variable is entered as a numeric scale variable in R. You'll begin by using a histogram to visually explore the velocity of Granke's pitches. In later exercises, you'll describe the data numerically. A histogram is a basic visualization tool for exploring the characteristics of your data using all of the start speed data. It's easy to plot a histogram in R with the code here and get a very basic looking plot. You'll improve on the look of this plot in the exercises. You can also indicate where the overall average start speed is on your histogram using the abline function. In this case, you want to tell R to draw a vertical line using the V is equal to parameter. We want to make V equal to the mean start speed in the Granke data set. Let's also color the line red so it's easy to see on our histogram. Something else to notice about this figure is that it can be useful in identifying multimodal distributions. This could indicate some separation in velocity related to the type of pitch thrown. This is easy to see here where it looks like Granke has a higher velocity distribution for fastballs and a separate lower velocity distribution for off speed pitches. You can identify pitch type in the data with the pitch type variable and make a separate histogram of each pitch type. Here let's just create a histogram for sliders represented by the SL code in the pitchtype variable. First, we'll use the if else function to make a new variable called slider. The if else function simply tells R that if the pitch type variable is equal to sl, then we want our new variable to be equal to 1. Otherwise we make the variable equal to 0. Notice that the ones in the new variable line up perfectly with the SL code in the pitch type variable. You could also make a variable called notslider. In this case you would tell R that we want this variable equal to 1 if if pitch type does not equal slider and 0. Otherwise you can see the desired result here. Any pitch type that is not a slider is equal to 1 in the not slider variable and any pitch type that is a slider is equal to 0. Now that we've made a new variable to indicate a pitch was a slider. We can use this to easily subset our data. The subset function is an easy way to do this. Naming the new data set crankysl we tell R to keep any data where the slider variable is equal to 1. Notice here that our new data includes only sliders. Further, note that within the subset function you already denote what data is being subset and therefore when you give R the condition for the subsetting, you do not have to use the data name and the dollar sign to choose your vector. Granted, the original if else was not necessary as we could have also subset by the pitch type variable in the first place and ended up with the same result. This makes subset pretty convenient when we want to work with portions of our data. Finally, when making a histogram of just sliders, we can see that the distribution of a single pitch type is much closer to a normal distribution than what we saw with all pitch types. Throughout the next few exercises you'll be performing similar operations to examine Granke's fast Hall velocity and compare July to other months of the year. Now start exploring your data.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







