R tutorial: Using tapply() for comparisons

Learn how to use R's tapply() function to calculate group summaries like mean pitch velocity by month or pitch type. The tutorial shows how to convert results into a tidy data frame, then build a layered time-series plot with average values as a line and individual pitches scattered with jitter() and transparency to reveal patterns in the data.
Chapters
In the previous exercise, breaking the data into different groups allowed you to compare distributions of velocity for July relative to other months in 2015, but it's often useful to also get numeric summaries of each group for comparison to use in more complex visualization in R. One way to do this is to use the tapply function. This function applies another function such as mean separately across groups, so such as month or pitch type. Tapply simply requires that you first tell R the variable you want to summarize, the grouping variable, and the function to apply across each group. For example, if you want to get the average pitch velocity by month, you would indicate that the variable of interest is start speed, the month variable is the group, and you want to calculate the mean. You can format the tapply output as a dataframe by wrapping it within the dataframe function. This allows you to name the new summary vectors as variables, just like in any data set. This can simplify later code and allow you to treat your data just like you would any other data frame. In the upcoming exercises, you'll also use tapply in combination with the plot function to create a time series figure that shows the fast fall velocity across each game in the season. Let's start by plotting a Y variable, so start speed by an X variable the month. You can see the points plotted here. However, time series plots are usually created as line plots. This is coded simply with the plot function using the Type L option. While line plots are useful for recognizing trends in the mean, it's sometimes helpful to visualize the variability for each game as well. Therefore, you'll also add individual points representing the velocity of each pitch to evaluate the variability in velocity for each game. Since There are about 100 pitches for each game and velocity is measured at one tenth of a mile per hour, many points will be overlapping in the plot. This can make it difficult to see all of the pitches. As you can see here, we can fix this with the use of the jitter function wrapped within the points function to add them to your plot. The jitter function is useful when you have many individual points overlapping one another jitter slightly spreads these out either across the y axis, the x axis, or both, so that you can have a better idea of the number of pitches in each game. When you start your exercises, you'll slowly build up a plot similar to this one, but you'll also pair jitter with transparent colors in the plot. Using a combination of these two visualization strategies can help communicate your data more efficiently. Go ahead and get started.
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







