Question

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?

Was it helpful?

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

OTHER TIPS

Sacha has already given you a working answer, but for future reference, you may want to familiarise yourself with the cut function, which is designed to break up a continuous variable into chunks.

x <- cut(x, c(-Inf, 0:5, Inf), labels=c(0:5, ">5"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top