Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index data frame column by a variable?

Tags:

types

dataframe

r

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])
   print(columnClass)
 }

}

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

like image 319
User Avatar asked Dec 27 '22 04:12

User


1 Answers

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.

like image 58
IRTFM Avatar answered Jan 10 '23 13:01

IRTFM