0

How to find the data type of a column in a data.frame?

I am using the below code, does it make sense? Am I getting the correct output.

for (f in feature.names) {

  if (class(train[[f]]) == "character") {
    count_c <- count_c + 1
  }
  if(class(train[[f]]) == "numeric"){
    count_numeric <- count_numeric + 1
  }
  if(class(train[[f]]) == "logical"){
    print(f)
    print(unique(train[[feature.names[count_n]]]))
    cat('\n')
    count_logic <- count_logic + 1
  }
  if(class(train[[f]]) == "integer"){
    count_int <- count_int + 1
  }
  count_n <- count_n + 1
}
Anoop Toffy
  • 918
  • 1
  • 9
  • 22
  • Tha above code should give me different data types count present in the data.frame – Anoop Toffy Aug 22 '15 at 03:10
  • 2
    You can get the class of a dataset using `ind <- sapply(yourdat, class)`. The output will be a vector. You can use the conditions based on the 'ind'. – akrun Aug 22 '15 at 03:31

1 Answers1

1

Provided you have initialized your counters properly, your code should work. (A reproducible example would be nice ...) Following up on @akrun's suggestion,

table(sapply(yourdat, class))

should replace what you're doing, although something like

allClasses <- c("logical","integer","character","numeric")
s <- sapply(yourdat,class)
f <- factor(s,levels=allClasses)
table(f)

might work slightly better (will work if there are types not represented in the data set).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453