1 | initial version |
library(dplyr)
library(ggplot2)
data_summary <- function(data, group_var){
# Group Data by the given Grouping Variable
data %>%
group_by(!!sym(group_var)) %>%
summarize(
mean_val = mean(value),
median_val = median(value),
max_val = max(value),
min_val = min(value)
) %>%
# Create a Boxplot for the Summary Statistics
ggplot(aes(x = !!sym(group_var), y = value)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(alpha = 0.3) +
labs(x = group_var, y = "Value")
}
The function first groups the data by the specified column ('group_var') and then calculates summary statistics (mean, median, max, and min) for each group using dplyr functions. Finally, the function creates a boxplot of the summary statistics using ggplot2 functions.
Test the function with some data:
# Create Sample Data Frame
df <- data.frame(
group = rep(LETTERS[1:3], each = 20),
value = rnorm(60)
)
# Call the Function
data_summary(df, "group")
This code creates a sample data frame with 3 groups and 20 observations per group. The 'data_summary' function is then called with this data and the column name 'group' as the grouping variable. The function outputs a boxplot illustrating the summary statistics for each group.