Hi there, probably there is a very simple solution, but I cannot think of one... I have a vector with values: data <- c(1,6,3,4,8,4,2,9) and I have a vector with bin breaks: bins <- c(1,3,5,7,9,11) Now, I'd like to get for each data point the index of the bin-vector where the value falls in (or equals the lower bin break). In the example case, I'd like to get: c(1,3,2,2,4,1,5) How would you solve it? Antje
Am 19.03.2011 13:18, schrieb Antje Niederlein:> Hi there, > > probably there is a very simple solution, but I cannot think of one... > I have a vector with values: > data<- c(1,6,3,4,8,4,2,9) > and I have a vector with bin breaks: > bins<- c(1,3,5,7,9,11)cut() does what you want: > cut(data, bins) [1] <NA> (5,7] (1,3] (3,5] (7,9] (3,5] (1,3] (7,9] Levels: (1,3] (3,5] (5,7] (7,9] (9,11] > as.numeric(cut(data,bins)) [1] NA 3 1 2 4 2 1 4 data[1] is NA because of the structure of the intervals. Gr?sse, Alex
On 11-03-19 8:18 AM, Antje Niederlein wrote:> Hi there, > > probably there is a very simple solution, but I cannot think of one... > > I have a vector with values: > > data<- c(1,6,3,4,8,4,2,9) > > and I have a vector with bin breaks: > > bins<- c(1,3,5,7,9,11) > > Now, I'd like to get for each data point the index of the bin-vector > where the value falls in (or equals the lower bin break). > > In the example case, I'd like to get: > > c(1,3,2,2,4,1,5) > > How would you solve it?Use cut. For example, data <- c(1,6,3,4,8,4,2,9) bins <- c(1,3,5,7,9,11) result <- cut(data, breaks=bins, right=FALSE) as.numeric(result) Duncan Murdoch