Reputation: 17617
I have an R data frame and some of the variables are categorical. For example sex is "male" or "female" and "do you smoke" is 0 or 1. Others variables instead are continuous. I would like to know if there is any way to decide if a variable is categorical or not and in case compute its frequencies.
I think in my case a good test would be to check if the variable takes less than k=4 values.
Upvotes: 5
Views: 26275
Reputation: 71
You can use class(dataframe$variable)
to know the class of a variable within a data frame as well as determine whether the variable is a factor or not.
Upvotes: 3
Reputation: 42679
While you should use factors for categorical variables, you can find the unique values in a vector x
with unique
, and count them:
length(unique(x))
Upvotes: 5