Q:

[[ ]] vs $: why doesn't the latter work on this Datacamp code?

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)

A:

There are various ways to access elements in dataframes. This is an issue with the way R is looking for the column names you want it to find.
One way is what datacamp showed, using data[[col]]. Another is the $ accessor, as in data$col. The latter does not substitute variables from functions on the fly. It's looking for a column literally called "col", and the error is reporting that it found no such column. On the other hand, the way datacamp accesses these columns, it was able to find "trip_distance", "total_amount", and "passenger_count".

