How to check if a variable is categorical with R?

I have an R data frame , and some of the variables are categorical. For example, sex is โ€œmanโ€ or โ€œwoman,โ€ and โ€œyou smokeโ€ is 0 or 1. Other variables are continuous instead. I would like to know if there is a way to decide whether a variable is categorical or not, and if its frequencies are calculated.

I think in my case a good test would be to check if a variable accepts less than k = 4.

+4
source share
1 answer

While you should use factors for categorical variables, you can find unique values โ€‹โ€‹in the vector xwith uniqueand calculate them:

length(unique(x))
+3
source

All Articles