从R中的整数列创建分类值列
我对R很陌生,但我还没有找到一个简单的解决方案。例如,我有以下数据帧:从R中的整数列创建分类值列,r,range,R,Range,我对R很陌生,但我还没有找到一个简单的解决方案。例如,我有以下数据帧: case <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) areas <- c(1,2,1,1,1,2,2,2,2,1,1,2,2,2,1,1,1,2,2,2) A <- c(1,2,11,12,20,21,26,43,43,47,48,59,63,64,65,66,67,83,90,91) var <- c(1,1,0,0,0,1,1
case <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
areas <- c(1,2,1,1,1,2,2,2,2,1,1,2,2,2,1,1,1,2,2,2)
A <- c(1,2,11,12,20,21,26,43,43,47,48,59,63,64,65,66,67,83,90,91)
var <- c(1,1,0,0,0,1,1,0,0,1,0,1,0,1,1,0,0,0,0,0)
outcome <- c(1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0)
df <- data.frame(case,areas,A,var,outcome)
case areas A var outcome
1 1 1 1 1 1
2 2 2 2 1 0
3 3 1 11 0 0
4 4 1 12 0 0
5 5 1 20 0 0
6 6 2 21 1 0
7 7 2 26 1 0
8 8 2 43 0 0
9 9 2 43 0 0
10 10 1 47 1 1
11 11 1 48 0 0
12 12 2 59 1 1
13 13 2 63 0 0
14 14 2 64 1 0
15 15 1 65 1 0
16 16 1 66 0 0
17 17 1 67 0 0
18 18 2 83 0 1
19 19 2 90 0 0
20 20 2 91 0 0
case您可以使用cut()
或findInterval()
breaks=c(0,5,20,50,80,Inf)
labels=c(“您可以使用cut()
或findInterval()
breaks=c(0,5,20,50,80,Inf)
labels=c(“您正在寻找cut()
函数。您希望基于间隔创建一个因子,这是该函数提供的
df$new_factor <- cut(df$A, breaks = c(-Inf, 5, 20, 50, 80, Inf),
labels = c('<5', '5-19', '20-49', '50-79', '80+'),
right = FALSE)
df$new\u factor您正在查找cut()
函数。您希望基于间隔创建一个因子,这是该函数提供的
df$new_factor <- cut(df$A, breaks = c(-Inf, 5, 20, 50, 80, Inf),
labels = c('<5', '5-19', '20-49', '50-79', '80+'),
right = FALSE)
df$new\u查看cut()
。查看cut()
。
df$new_factor <- cut(df$A, breaks = c(-Inf, 5, 20, 50, 80, Inf),
labels = c('<5', '5-19', '20-49', '50-79', '80+'),
right = FALSE)