In order to summarize multiple variables by group in R, we can use the dplyr
package.
Assuming we have a data.frame
called my_data
with variables group
, var1
, var2
, var3
, and var4
, we can group the data by the group
variable and then summarize the other variables using the summarize
function.
main.r322 chars13 lines
In this code block, we first load the dplyr
package. Then, we use the pipe operator %>%
to chain a series of functions together. We first group the data by the group
variable using group_by
. We then summarize the data using summarize
and calculate the mean and standard deviation of each variable for each group.
The resulting output will be a data.frame
with one row for each group and one column for each summarized variable.
gistlibby LogSnag