Search code examples
rintervals

How to make a new variable that assigns a numeric variable to intervals of 1000 units. R


In a dataframe I have the variable position that ranges from 0 to 2.7M.

I want to create a new variable bin that takes the value of position and it assigns it to intervals of 1000:

  • From 1 to 1000 -> 1000
  • From 1001 to 2000 -> 2000
  • From 2001 to 3000 -> 3000
  • etc
position bin
128 1000
333 1000
2900 3000
4444 5000

I have looked at previous questions and couldn't find a solution.

Thanks in advance.


Solution

  • You could use

    df$bin_2 <- (df$position %/% 1000 + 1) * 1000