The “summarize_all” Function in R
Package: dplyr
Purpose: To apply a summarization function to all columns in a data frame.
General Class: Data Manipulation
Required Argument(s):
data: The data frame to summarize.
list(): A list of summary functions to apply to each column.
Notable Optional Arguments:
None.
Example (with Explanation):
# Load necessary packages
library(dplyr)
# Create a sample data frame
data <- data.frame(
ID = c(1, 2, 3),
value1 = c(10, 15, 20),
value2 = c(25, 30, 35)
)
# Summarize all numeric columns by calculating mean and sum
summary_result <- data %>%
summarize_all(list(mean = mean, sum = sum))
# Display the summary result
print(summary_result)In this example, the summarize_all function from the dplyr package is used to apply two summarization functions (mean and sum) to all numeric columns in the sample data frame data. The result, summary_result, provides the mean and sum for each numeric column.