Input column name as variable in a function

The dollar operator does not work with variables. You can use double square brackets instead:

col_name <- "mpg"
mtcars[[col_name]]

# compare:
mtcars$mpg

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top