我正在尝试重新分配原始分数。为了了解更多的背景,我让10个人在0-100的范围内给100个项目打分。我想做的是将原始分数重新分组为1-5。例如,将原始分数0-20 = 1;21-40 = 2;41-60 = 3;61-80 = 4;81-100 = 5。我是Rstudio的新手,不知道该怎么做。
我们可以用cut
as.integer(cut(scores, breaks = c(-Inf, 20, 40, 60, 80, 100)))
或与findInterval
findInterval(scores, c(0, 20, 40, 60, 80, 100))
数据set.seed(24)
scores <- sample(0:100, 20, replace = TRUE)
您可以将分数除以20并使用ceiling
。
df$group <- ceiling(df$score/20)
df
# people item score group
#1 1 3 90 5
#2 2 10 91 5
#3 3 2 69 4
#4 4 8 99 5
#5 5 6 57 3
#6 6 9 92 5
#7 7 1 9 1
#8 8 7 93 5
#9 9 5 72 4
#10 10 4 26 2
如果您以可重复的格式提供数据,则更容易提供帮助
set.seed(123)
df <- data.frame(people = 1:10, item = sample(10), score = sample(100, 10))