0

how to obtain the count of numeric variables in a csv file using R So I'm having 28 columns with numeric variables and some categorial variables. I know I could use class(datasheet$variablename) to find out if a specific variable is integer or factor. But I have multiple columns and I'm wondering if there's a quicker way to count how many numeric variables do I have in total.

Completely new to R. Really just expecting some beginner functions and coding. Any advice will help and really appreciate!

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Zyan_C
  • 3
  • 1
  • Do you mean `str(datasheet)` ? – jared_mamrot Jan 30 '21 at 22:58
  • 1
    Maybe `table(sapply(datasheet, class))`? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 30 '21 at 22:59
  • Yeah thanks Jared, I tried str() function and this looks like a list of the data types of all the variables. It is fine but I just needed counts of each class. – Zyan_C Jan 30 '21 at 23:20
  • And MrFlick that table is perfect and thank you so much! – Zyan_C Jan 30 '21 at 23:21

1 Answers1

0

try

datasheet<-read.csv('filename.csv')
sapply(datasheet, is.numeric)

to tell which variables are numeric

sum(sapply(datasheet, is.numeric))

will tell you how many are numeric

GuedesBF
  • 8,409
  • 5
  • 19
  • 37