Search code examples
ranglepolar-coordinatessector

assign a sector to an angle in R


I have a vector of start and end angels values between 0 and 360. I would like to have an additional column that specifies in which sector(consider 12 sectors) are my variables.

sectors should be defined as : (15:45], (45,75],(75,105], ..., (345,15]

test = structure(list(start = c(4, 67, 13, 35, 54, 0), end = c(23, 84, 
30, 52, 71, 0)), row.names = 2:7, class = "data.frame")

For my test example I thought I have to loop over the number of rows :

for( i in 1:nrow(test)){
  if(test$start[i] <= 15 | test$start[i] >345){test$sector_start[i] = 12}
  else if(test$start[i] > 15 & test$start[i] <= 45){test$sector_start[i] = 1}
  else if(test$start[i] > 45 & test$start[i] <= 75){test$sector_start[i] = 2}
  else if(test$start[i] > 75 & test$start[i] <= 105){test$sector_start[i] = 3}
  else if(test$start[i] > 105 & test$start[i] <= 135){test$sector_start[i] = 4}
  else if(test$start[i] > 135 & test$start[i] <= 165){test$sector_start[i] = 5}
  else if(test$start[i] > 165 & test$start[i] <= 195){test$sector_start[i] = 6}
  else if(test$start[i] > 195 & test$start[i] <= 225){test$sector_start[i] = 7}
  else if(test$start[i] > 225 & test$start[i] <= 255){test$sector_start[i] = 8}
  else if(test$start[i] > 255 & test$start[i] <= 285){test$sector_start[i] = 9}
  else if(test$start[i] > 285 & test$start[i] <= 315){test$sector_start[i] = 10}
  else if(test$start[i] > 315 & test$start[i] <= 345){test$sector_start[i] = 11}

  if(test$end[i] <= 15 | test$end[i] >345){test$sector_end[i] = 12}
  else if(test$end[i] > 15 & test$end[i] <= 45){test$sector_end[i] = 1}
  else if(test$end[i] > 45 & test$end[i] <= 75){test$sector_end[i] = 2}
  else if(test$end[i] > 75 & test$end[i] <= 105){test$sector_end[i] = 3}
  else if(test$end[i] > 105 & test$end[i] <= 135){test$sector_end[i] = 4}
  else if(test$end[i] > 135 & test$end[i] <= 165){test$sector_end[i] = 5}
  else if(test$end[i] > 165 & test$end[i] <= 195){test$sector_end[i] = 6}
  else if(test$end[i] > 195 & test$end[i] <= 225){test$sector_end[i] = 7}
  else if(test$end[i] > 225 & test$end[i] <= 255){test$sector_end[i] = 8}
  else if(test$end[i] > 255 & test$end[i] <= 285){test$sector_end[i] = 9}
  else if(test$end[i] > 285 & test$end[i] <= 315){test$sector_end[i] = 10}
  else if(test$end[i] > 315 & test$end[i] <= 345){test$sector_end[i] = 11}
}

here I could add 2 column to test which is telling me my angles are in which sector. I'm looking for a smarter way of doing this that I could have an option to vary the number of sectors, for example to 24 sectors.


Solution

  • As Roman says, you can use cut. The last step is for angles > 345 or <= 15.

    library(dplyr)
    test %>% 
      mutate(sector_start = cut(start, 15 + 30*(0:11), 1:11)
             , sector_end = cut(end, 15 + 30*(0:11), 1:11)) %>% 
      mutate_at(vars(contains('sector')), ~ifelse(is.na(.), 12, .))
    

    In base R:

    test[paste0('sector_', names(test))] <- 
      lapply(test, function(x){
        labs <- cut(x, 15 + 30*(0:11), 1:11)
        ifelse(is.na(labs), 12, labs)
      })