Group by Rows
Learn how to group together rows and create new meaningful analysis.
Example with the temperature
dataset
Instead of a single mean temperature for the whole year, we would like 12 mean temperatures, one for each of the 12 months separately. In other words, we would like to compute the mean temperature split by month. We can do this by grouping temperature observations by the values of another variable, and in this case, by the 12 values of the variable month
. Run the following code:
Press + to interact
summary_monthly_temp <- weather %>% group_by(month) %>%summarize(mean = mean(temp, na.rm = TRUE),std_dev = sd(temp, na.rm = TRUE))summary_monthly_temp
This code is identical to the previous code that created summary_temp
, but with an extra group_by(month)
added before the summarize()
. Grouping the weather
dataset by month and then applying the summarize()
function ...