I am working through Datacamp's Intro to R course, but I do not understand why this code works:
# Define columns
columns <- c("trip_distance", "total_amount", "passenger_count")
# Create summary function
taxis_summary <- function(col, data = taxis) {
c(
mean = mean(data[[col]]),
sd = sd(data[[col]]),
quantile(data[[col]], c(0.25, 0.5, 0.75))
)
}
# Use sapply to summarize columns
sapply(columns, taxis_summary)
but this code throws a:
Unknown or uninitalised column: 'col'. Argument is not numeric or logical: returning NA
# Define columns
columns <- c("trip_distance", "total_amount", "passenger_count")
# Create summary function
taxis_summary <- function(col, data = taxis) {
c(
mean = mean(data$col),
sd = sd(data$col),
quantile(data$col, c(0.25, 0.5, 0.75))
)
}
# Use sapply to summarize columns
sapply(columns, taxis_summary)