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"
colnames
to get the column names of df
. 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]])))}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With