Reputation: 920
I am trying to create a label with cut
and here is the example
> cut(c(1,5,10,15,160),c(0,5,10,15,Inf))
[1] (0,5] (0,5] (5,10] (10,15] (15,Inf]
Levels: (0,5] (5,10] (10,15] (15,Inf]
I wanted to automatically create labels like this
"1~5" "6~10" "11~15" "15+"
Is there any way I can do it automatically?
Upvotes: 2
Views: 257
Reputation: 388797
If you want to do this automatically without manually specifying the labels you can do the manipulation as :
vec <- c(1,5,10,15,160)
breaks <- c(0,5,10,15,Inf)
n <- length(breaks)
labels <- paste(breaks[-n] + 1, breaks[-1], sep = '~')
labels[length(labels)] <- paste0(breaks[n - 1], '+')
cut(vec,breaks, labels)
#[1] 1~5 1~5 6~10 11~15 15+
#Levels: 1~5 6~10 11~15 15+
Upvotes: 2