1

When I cut one vector by another vector, I get a lot of factors like this: (0,100], (100,200], etc. Is there any way to get one of these factors and then take the upper or lower bound?

ex: for (100,200] I want some way to grab the 100 and the 200.

Edit:

> v <- cut(c(3,4,6,8), c(1,4,5,9,12))  
> v  
[1] (1,4] (1,4] (5,9] (5,9]  
  Levels: (1,4] (4,5] (5,9] (9,12]  
  > v[1]  
  [1] (1,4]  
  Levels: (1,4] (4,5] (5,9] (9,12]

I want some way, when given v[1], to get the numbers 1 and 4.

4

2 回答 2

2
v <- cut(c(3,4,6,8), c(1,4,5,9,12))
levels(v)
#[1] "(1,4]"  "(4,5]"  "(5,9]"  "(9,12]"

as.numeric(sub('.([^,]+),.*', '\\1', levels(v)))
#[1] 1 4 5 9

as.numeric(sub('.*,(.*).', '\\1', levels(v)))
#[1]  4  5  9 12
于 2013-07-11T19:24:20.043 回答
2
strsplit(gsub("\\[|\\]|\\(", "", as.character(v[1]) ) , ",")[[1]]

#[1] "1" "4"
于 2013-07-11T19:28:06.540 回答