R – How to index data frame column by a variable


As an example, I want a function that will iterate over the columns in a dataframe and print out each column's data type (e.g., "numeric", "integer", "character", etc)

Without a variable I know I can do class(df$MyColumn) and get the data type. How can I change it so "MyColumn" is a variable?

What I'm trying is

f <- function(df) {

 for(column in names(df)) {
   columnClass = class(df[column])


But this just prints out [1] "data.frame" for each column.

Best Solution

Since a data frame is simply a list, you can loop over the columns using lapply and apply the class function to each column:

lapply(df, class)

To address the previously unspoken concerns in User's comment.... if you build a function that does whatever it is that you hope to a column, then this will succeed:

func <- function(col) {print(class(col))}
lapply(df, func)

It's really mostly equivalent to:

 for(col in names(df) ) { print(class(df[[col]]))} 

And there would not be an unneeded 'colClass' variable cluttering up the .GlobalEnv.