Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through dataframe column names - R

Tags:

loops

dataframe

r

I'm trying to loop through the columns names of a dataframe, and evaluate which class is each column.

for (i in columns(df)){
  class(df$i)
}

I have tried everything, except the right way..

PS: I'm trying to do in this way because after I have to put different conditions for each class.

like image 561
Chris Avatar asked Apr 18 '18 00:04

Chris


Video Answer


2 Answers

To answer the exact question and fix the code given, see the example below

df <- iris # data

for (i in colnames(df)){
   print(class(df[[i]]))
}
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "numeric"
# [1] "factor"
  1. you need to used colnames to get the column names of df.
  2. you access each column using df[[i]] if you want to know the class of that. df[i] is of class data.frame.
like image 68
tpol Avatar answered Oct 19 '22 06:10

tpol


The problem was to loop through the columns of a dataframe, and an additional question was asked about looping through some subset of the dataframe. I used the mtcars dataset because it has more columns of data than the iris dataset. This allowed for a richer example. To loop through some subset of columns, use a numerical value in a for loop rather than using the names of the columns. If columns of interest are regularly spaced then make a vector with the columns of interest. Examples follow:

#Similar to previous answer only with mtcars rather than iris data.
df2<-mtcars
for (i in colnames(df2)){print(paste(i,"  ",class(df2[[i]])))}

#An alternative that is as simple but does not also print the variable names.
df2<-mtcars
for (i in 1:ncol(df2)){print(paste(i,"  ",class(df2[[i]])))}

#With variable names:
df2<-mtcars
for (i in 1:ncol(df2)){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#Now that we are looping numerically one can start in column 3 by:
df2<-mtcars
for (i in 3:ncol(df2)){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#To stop before the last column add a break statement inside an if
df2<-mtcars
for (i in 3:ncol(df2)){
  if(i>7){break}
  print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}

#Finally, if you know the columns and they are irregularly spaced try this:
UseCols<-c(2,4,7,9,10)
for (i in UseCols){print(paste(i,"   ",colnames(df2[i]),"  ",class(df2[[i]])))}
like image 1
TimothyEbert Avatar answered Oct 19 '22 05:10

TimothyEbert