r - Cut vector into groups -


a <- -30:30 cutsize <- 10 b <- %/% cutsize table(b) # output -3 -2 -1  0  1  2  3  10 10 10 10 10 10  1  

wanted output

-3 -2 -1  0  1  2  3  10 10 10  1 10 10  10 

i need divide vector groups (by cutsize). used %/%, apparently "shifts" groups. want group:

  • zero group 0
  • everything 1 10 group 1
  • from 11 20 group 2
  • ...

here's bit awkword solution

table(ceiling((a / cutsize) * sign(a)) * sign(a)) # -3 -2 -1  0  1  2  3  # 10 10 10  1 10 10 10  

or similarly

table(ceiling(abs(a / cutsize)) * sign(a)) # -3 -2 -1  0  1  2  3  # 10 10 10  1 10 10 10  

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -