Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 778
user1021000 Avatar asked Jan 23 '26 10:01

user1021000


1 Answers

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

like image 174
Sacha Epskamp Avatar answered Jan 26 '26 00:01

Sacha Epskamp