0

I have a dataframe(df). I need to find the class of each column names in a for loop. I tried with the below code but not able to get the solution

> df
  x y g
1 a 2 1
2 a 3 2
3 b 4 1
4 b 5 2

>for(i in colnames(df))
 {
 print(class(df$i)) }

The expected out put is

> "factor","integer","integer"

Because class of x is a factor and other 2 are integers

Rfer R
  • 69
  • 5

1 Answers1

0

If you want to achieve that with a loop,

for(i in colnames(df))
 {
 print(class(df[,i])) 
}

But actually you can do the very same thing with sapply as,

saplly(df,class)

maydin
  • 3,715
  • 3
  • 10
  • 27