Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 "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.

like image 737
Donbeo Avatar asked Jan 05 '14 15:01

Donbeo


People also ask

How do you know if a variable is categorical R?

In descriptive statistics for categorical variables in R, the value is limited and usually based on a particular finite group. For example, a categorical variable in R can be countries, year, gender, occupation. A continuous variable, however, can take any values, from integer to decimal.

How do you know if a variable is categorical or numerical?

Answer. A categorical variable is a variable with a set number of groups (gender, colors of the rainbow, brands of cereal), while a numeric variable is generally something that can be measured (height, weight, miles per hour).

How do I know the type of a variable in R?

To check the data type of a variable in R, use the typeof() function. The typeof() is a built-in R function that defines the (internal) type or storage mode of any R object.

How do you know if a data set is categorical?

If the data can only be grouped into categories, then it is considered a categorical variable. If, however, if you can perform arithmetic operations then it is considered a numerical or quantitative variable. For example, a random group of people could be surveyed: To determine their grade point average.


1 Answers

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))
like image 180
Matthew Lundberg Avatar answered Sep 17 '22 00:09

Matthew Lundberg