Search code examples
rgroupingr-factor

R grouping data with factors and levels


I'm trying to make a frequency table that groups values into a limited number of bins.

Say I have the data

X <- c(1,2,3,4,3,9, 20)

I can make a frequency table such that it shows all the empty cells like this:

(factor(X, levels = c(0:max(X))))

Instead of showing the frequency of every possible value, I would like to bin values >5 so that the levels on the table are: 0, 1, 2, 3, 4, 5, and >5.

How can I do this?


Solution

  • You first need to transform the vector so that it has an unique entry for, then you can add the missing levels in the factor() function:

    X <- c(1,2,3,4,3,9,20)
    X <- ifelse(X>5,">5",X)
    X <- factor(X,levels=c(0:5,">5"))
    

    This results in:

    X [1] 1 2 3 4 3 >5 >5 Levels: 0 1 2 3 4 5 >5