Search code examples
rlevels

how to substract numbers from levels


I would like to cut a vector of values ranging 0-70 to x number of categories, and would like the upper limit of each category. So far, I have tried this using cut() and am trying to extract the limits from levels. I have a list of levels, from which I would like to extract the second number from each level. How can I extract the values between space and ] (which is the number I'm interested in)?

I have:

> levels(bins)
 [1] "(-0.07,6.94]" "(6.94,14]"    "(14,21]"      "(21,28]"      "(28,35]"     
 [6] "(35,42]"      "(42,49]"      "(49,56]"      "(56,63.1]"    "(63.1,70.1]" 

and would like to get:

[1] 6.94 14 21 28 35 42 49 56 63.1 70.1

Or is there a better way of calculating the upper bounds of categories?


Solution

  • This could be one solution

    k <- sub("^.*\\,","", levels(bins))
    as.numeric(substr(k,1,nchar(k)-1))
    

    gives

     [1]  6.94 14.00 21.00 28.00 35.00 42.00 49.00 56.00 63.10 70.10