r - Categorize a vector according to intervals -


this question has answer here:

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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -