Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert continuous variable to discrete in R?

I have a variable which encodes group ID:

d <- data.frame(group = c(0,1,0,2,1,3,2,0,1,2), x=c(1.2,2.3,3.2,2.1,1.3,1.5,2.3,0.4,1.3,1.7))

When I try to use it in ggplot2 for making boxplots I get an error

Continuous value supplied to discrete scale

At attempt to render data. Then I manually change at least one group ID in data to text everything works OK.

So, my question is: is where some easy way to change continuous variable, containing finite number of variants to discrete?

like image 764
Yuriy Petrovskiy Avatar asked Apr 22 '13 13:04

Yuriy Petrovskiy


People also ask

How do you make a continuous variable discrete?

We often convert continuous variables into discrete ones. We do this by dividing up the continuous variable into ranges of values. We then assign the same discrete value to all values of the continuous variable that fall within a certain range.

Can you convert continuous data to discrete data?

Discretization is the process through which we can transform continuous variables, models or functions into a discrete form. We do this by creating a set of contiguous intervals (or bins) that go across the range of our desired variable/model/function. Continuous data is Measured, while Discrete data is Counted.

How do you convert continuous data to categorical 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 bin a continuous value in R?

To create the bins for a continuous vector, we can use cut function and store the bins in a data frame along with the original vector. The values in the cut function must be passed based on the range of the vector values, otherwise, there will be NA's in the bin values.


2 Answers

this:

ggplot(d) + geom_boxplot(aes(factor(group), x))

gives the following plot

enter image description here

like image 117
Michele Avatar answered Oct 11 '22 03:10

Michele


Since you're providing the group variable with a numeric vector, this is understood as a continuous variable. You need to convert it to a categorical variable. Try the following:

d <- data.frame(group = as.factor(c(0,1,0,2,1,3,2,0,1,2)), x=c(1.2,2.3,3.2,2.1,1.3,1.5,2.3,0.4,1.3,1.7))

The as.factor function will convert the numeric vector you provided for the groups to a discrete variable.

like image 31
João Daniel Avatar answered Oct 11 '22 03:10

João Daniel