Ahdee
Ahdee

Reputation: 4949

Split dataframe into bins based on another vector

suppose I have the following dataframe

x <- c(12,30,45,100,150,305,2,46,10,221)
x2 <- letters[1:10]
df <- data.frame(x,x2)
df <- df[with(df, order(x)), ]
     x x2
7    2  g
9   10  i
1   12  a
2   30  b
3   45  c
8   46  h
4  100  d
5  150  e
10 221  j
6  305  f

And I would like to split these into groups based on another vector,

v <- seq(0, 500, 50)

Basically, I would like to partition out each row based on column x and how it matches with to v ( so for example x <= an element in v) - the location/index of that element in v is then used to assign a group for that row. The resulting table should look something like the following:

     x x2 group
7    2  g    g1
9   10  i    g1
1   12  a    g1
2   30  b    g1
3   45  c    g1
8   46  h    g2
4  100  d    g3
5  150  e    g4
10 221  j    g4
6  305  f    g6

I could try to loop through each row and try and match it to v but I'm still confuse as to how I could easily detect where the match x<=element v occurs so that I can assign a group id to it. thanks.

Upvotes: 0

Views: 924

Answers (1)

Mark Timms
Mark Timms

Reputation: 606

You can use cut to break up df$x by the values of v:

df$group <- as.numeric(cut(df$x, breaks = v))
df$group <- paste0('g', df$group)

cut returns a factor so you can use as.numeric to just pull out which numeric bucket the value of df$x falls into based on v.

Upvotes: 3

Related Questions