Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create categorical variable in R based on range

Tags:

r

r-faq

I have a dataframe with a column of integers that I would like to use as a reference to make a new categorical variable. I want to divide the variable into three groups and set the ranges myself (ie 0-5, 6-10, etc). I tried cut but that divides the variable into groups based on a normal distribution and my data is right skewed. I have also tried to use if/then statements but this outputs a true/false value and I would like to keep my original variable. I am sure that there is a simple way to do this but I cannot seem to figure it out. Any advice on a simple way to do this quickly?

I had something in mind like this:

x   x.range
3   0-5
4   0-5
6   6-10
12  11-15
like image 238
Stedy Avatar asked Apr 15 '10 17:04

Stedy


People also ask

Can you use range for categorical data?

Explanation: With quantitative data, the range is used to show the values that the data exists on. For example, the range in ages of everyone in the United States is about 113.9. However, with categorical data, range does not make sense.

How do you create a categorical variable from a continuous variable in R?

You can use the cut() function in R to create a categorical variable from a continuous one. Note that breaks specifies the values to split the continuous variable on and labels specifies the label to give to the values of the new categorical variable.

How do I convert numerical data to categorical data in R?

Firstly, we will convert numerical data to categorical data using cut() function. Secondly, we will categorize numeric values with discretize() function available in arules package (Hahsler et al., 2021).

How do I group categorical variables in R?

When working with categorical variables, you may use the group_by() method to divide the data into subgroups based on the variable's distinct categories. You can group by a single variable or by giving in multiple variable names to group by several variables.


1 Answers

x <- rnorm(100,10,10)
cut(x,c(-Inf,0,5,6,10,Inf))
like image 192
Ian Fellows Avatar answered Sep 21 '22 05:09

Ian Fellows