Create a function that accepts a data frame and a column name and calculates the mean of that column.
MediumProblem Statement
Create a function that accepts a data frame and a column name and calculates the mean of that column.
Sample Input
Data Frame: Name Age 1 John 25 2 Alice 30 3 Bob 22 Column: Age
Sample Output
Mean of Age: 25.67
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
# Predefined structure
calculate_column_mean <- function(df, column_name) {
# Your logic here
}
mean_age <- calculate_column_mean(data, "Age")
print(paste("Mean of Age:", round(mean_age, 2)))
Input
Data Frame: Name Age 1 John 25 2 Alice 30 3 Bob 22 Column: Age
Expected Output
Mean of Age: 25.67