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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-11-29 11:00:00 +0000
Seen: 13 times
Last updated: Oct 28 '22
What is the concept of a Type Theory that includes an Any/Variant data type?
How can one obtain live data from interactive broker through API?
What is the method to implement pagination for Firestore data in Flutter using ListView?
What is the reason for the submitted Django form's value being null in the database?
How can we create summary tables by using nested tibbles?
What is the process of moving information from one tab to another in Excel by transposing it?
What does "coxphw undefined columns selected" mean?
Is it not possible to change the data type of an array from 'O' to 'float64'?
How to perform a historical backfill from GA4 into BigQuery?