1

I have a dataframe with a column of values, and I want to create a new column splitting these values into cohorts depending on where they fall within a given vector of numbers.

So for example lets say I have the following dataframe

df <- data.frame(item  = c('a', 'b', 'c', 'd', 'e', 'f'),
                 value = c(5  , 14 , 3  , 22 , 30 , 0))

i.e.

  item value
1    a     5
2    b    14
3    c     3
4    d    22
5    e    30
6    f     0

then I want to split the value column into cohorts depending on its value and where the value falls in a vector, and create a new column showing these cohort. Its probably easier to explain using an example vector

e.g. say I had the vector

vec <- c(5, 10, 25)

Item 'a' would fall between 5 and 10 (lets say the lower bound is >= and upper is <), item 'b' between 10 and 25, item 'c' is less than 5 etc..., and my new dataframe would look something like this

     item value cohort
1    a     5    5-10
2    b    14    10-25
3    c     3    -5
4    d    22    10-25
5    e    30    20-
6    f     0    -5

Does anyone know the quickest and best way of doing this?

Thanks in advance

joran
  • 169,992
  • 32
  • 429
  • 468
user1165199
  • 6,351
  • 13
  • 44
  • 60

0 Answers0