0

I have a dataframe that looks like the following:

outcome  treatment
2        0.01
3        0.78
2        0.54
1        0.68
4        0.97
5        0.40
2        0.25

I would like to split the treatment variable into 4 evenly spaced bins. Assuming the treatment variable spans from 0 to 1, I would like the bins to be:

Bin 1: [0,0.25)
Bin 2: [0.25, 0.5)
Bin 3: [0.5, 0.75)
Bin 4: [0.75, 1.0)

The final dataframe should look like this:

outcome  treatment  bin 
2        0.01       1
3        0.78       3
2        0.54       3
1        0.68       3
4        0.97       4
5        0.40       2
2        0.25       2

I tried using the following:

split(df, cut(df$treatment, 4)) 

This didn't work because the bin outputs looked like (0,0.25] rather than [0, 0.25), and I didn't know how to change it.

I am open to any way of doing this, but I am most familiar with the tidyverse.

markus
  • 25,843
  • 5
  • 39
  • 58
melbez
  • 960
  • 1
  • 13
  • 36

0 Answers0