summarize list of variables by group in r

To summarize a list of variables by group in R, you can use the dplyr and tidyr packages. First, you need to load your data into a data frame. Then, you can use the group_by() function to group the data by a specified variable. Finally, you can use the summarize() function to calculate summary statistics for each group.

Here is an example code that groups a data frame by a variable called "group", and summarizes two other variables called "var1" and "var2" using the mean and standard deviation functions:

main.r
library(dplyr)
library(tidyr)

df <- read.csv("data.csv")

df_summary <- df %>%
  group_by(group) %>%
  summarize(
    mean_var1 = mean(var1),
    sd_var1 = sd(var1),
    mean_var2 = mean(var2),
    sd_var2 = sd(var2)
  )

df_summary
234 chars
16 lines

This code will create a new data frame called df_summary that shows the mean and standard deviation of var1 and var2 for each group in the original data frame.

gistlibby LogSnag