【问题标题】:assign a sector to an angle in R将扇区分配给R中的一个角度
【发布时间】:2019-02-24 00:09:30
【问题描述】:

我有一个 0 到 360 之间的开始和结束天使值向量。 我想要一个额外的列来指定哪个扇区(考虑 12 个扇区)是我的变量。

扇区应定义为:(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( 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}
}

在这里我可以在test 中添加 2 列,告诉我我的角度在哪个扇区。我正在寻找一种更智能的方法,我可以选择改变扇区的数量,例如 24 个扇区。

【问题讨论】:

  • 使用cut 创建扇区。
  • @RomanLuštrik,你能给我举个例子吗?在这种情况下我可以跳过 for 循环吗?

标签: r angle polar-coordinates sector


【解决方案1】:

正如 Roman 所说,您可以使用 cut。最后一步是角度 > 345 或

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, .))

在基础 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)
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多