简体   繁体   中英

Loop through dataframe column names - 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.

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 .

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]])))}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM