summarize variables by group in r

To summarize variables by group in R, you can use the dplyr package. The first step is to group the data by the variable you want to summarize by, using the group_by() function. Then, you can use the summarise() function to calculate summary statistics for each group.

For example, let's say we have a data frame df with variables group and value, and we want to calculate the mean and standard deviation of value for each group:

main.r
library(dplyr)

summary_df <- df %>%
  group_by(group) %>%
  summarise(mean_value = mean(value),
            sd_value = sd(value))
131 chars
7 lines

This code first groups df by the group variable using group_by(). Then, it calculates the mean (mean()) and standard deviation (sd()) of value for each group using summarise(). The resulting data frame summary_df will have one row for each unique value of group, with columns for the mean and standard deviation of value within each group.

gistlibby LogSnag