Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output a numeric value from 'cut()' in R

Tags:

I've read this question here: Group numeric values by the intervals

However, I would like to output a numeric (rather than a factor), specifically the numeric value of the lower and/or upper bounds (in separate columns)

In essence, this is right, except that the 'df$start' and 'df$end' are given as factors:

df$start <- cut(df$x, 
                breaks = c(0,25,75,125,175,225,299),
                labels = c(0,25,75,125,175,225),
                right = TRUE)

df$end <- cut(df$x, 
              breaks = c(0,25,75,125,175,225,299),
              labels = c(25,75,125,175,225,299),
              right = TRUE)

The use of 'as.numeric()' returns the level of the factor (i.e. values 1-6) rather than the original numbers.

Thanks!