r - Categorize a vector according to intervals -
this question has answer here:
- r - cut defined interval 2 answers
i have discrete variable p represents propabilites
p <- c(0.5, 0.4, 0.5, 0.1, 0.8, 0.9 )
i want rearrange variable , change ordinal
the categories :
01 less 0.2 02 0.2 0.4 03 more 0.4 0.6 04 more 0.6 0.8 05 more 0.8 1
you can use function cut
follows
p <- c(0.5, 0.4, 0.5, 0.1, 0.8, 0.9 ) cut(p, breaks = seq(0, 1, 0.2), include.lowest = true) #[1] (0.4,0.6] (0.2,0.4] (0.4,0.6] [0,0.2] (0.6,0.8] (0.8,1] #levels: [0,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8] (0.8,1]
if want change labels, try labels
argument, e.g.
cut(p, breaks = seq(0, 1, 0.2), labels = paste0("0", 1:5), include.lowest = true) #[1] 03 02 03 01 04 05 #levels: 01 02 03 04 05
Comments
Post a Comment