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

I read this question here: Group numeric values ​​using intervals

However, I would like to output a numerical (not a coefficient), in particular a numerical value of the lower and / or upper boundaries (in separate columns)

In essence, this is correct, except that "df $ start" and "df $ end" are specified 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)

Using 'as.numeric ()' returns the level of the factor (i.e., values ​​1-6), not the original numbers.

Thanks!

+4
source share
2 answers

, , , " ", df$x. , , ? , .

## Generate some example data
x = runif(5, 0, 300)
## Specify the labels
labels = c(0,25,75,125,175,225)
## Use cut as before
y = cut(x, 
    breaks = c(0,25,75,125,175,225,300),
    labels = labels,
    right = TRUE)

y , . ,

labels[as.numeric(y)]

labels(y)
+4

cut , . , findInterval .bincode.

set.seed(17)
df <- data.frame(x=300 * runif(100))

:

breaks <- c(0,25,75,125,175,225,299)
df$interval <- findInterval(df$x, breaks)
df$start <- breaks[df$interval]
df$end <- breaks[df$interval + 1]
+4

All Articles