R创建范围因子水平

时间:2016-10-12 18:59:10

标签: r labeling

我想自动执行以下程序:

从矢量中获取最小值和最大值,并根据特定步长定义从最小值到最大值的步长。现在,向量内的每个值都被分配到label(因子级别),该值落入此范围,例如当值为"20-30"时,27.45

目前我正在使用这个for-loop

for (label_willi_counter in 1:length(willi)) {
  if(willi[label_willi_counter] < 10)
    label_willi = c(label_willi, "0 - 10")
  else if(willi[label_willi_counter] < 20)
    label_willi = c(label_willi, "10 - 20")
  else if(willi[label_willi_counter] < 30)
    label_willi = c(label_willi, "50 - 30")
  else if(willi[label_willi_counter] < 40)
    label_willi = c(label_willi, "30 - 40")
  else if(willi[label_willi_counter] < 50)
    label_willi = c(label_willi, "40 - 50")
  else if(willi[label_willi_counter] < 60)
    label_willi = c(label_willi, "50 - 60")
  else if(willi[label_willi_counter] < 70)
    label_willi = c(label_willi, "60 - 70")
  else if(willi[label_willi_counter] < 80)
    label_willi = c(label_willi, "70 - 80")
  else if(willi[label_willi_counter] < 90)
    label_willi = c(label_willi, "80 - 90")
  else
    label_willi = c(label_willi, "90 - 100")
}

它确实有效,但我确信有更好的(可能更快)的方法来做到这一点。此示例已针对minimum = 0maximum = 100以及step-size = 10进行了修复。我正在寻找一种更通用的方法来做到这一点。

1 个答案:

答案 0 :(得分:4)

尝试cut

x
# [1] 60 35 21 30  6 19 49 17 59 93  9 96 46 63  3 58 13 86 47 16

cut(x, breaks = seq(0,100,10), labels = lbl)

#[1] 50-60  30-40  20-30  20-30  0-10   10-20  40-50  10-20  50-60  90-100 0-10   #90-100 40-50  60-70 
#[15] 0-10   50-60  10-20  80-90  40-50  10-20 
#Levels: 0-10 10-20 20-30 30-40 40-50 50-60 60-70 70-80 80-90 90-100

数据

x <- c(60L, 35L, 21L, 30L, 6L, 19L, 49L, 17L, 59L, 93L, 9L, 96L, 46L, 
63L, 3L, 58L, 13L, 86L, 47L, 16L)

lbl <- c("0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", 
"70-80", "80-90", "90-100")