cbind 基于值的新列
cbind new column based on values
我的数据是:
try<-data.frame(Gender=c("Male","Male","Female","Male","Female","Male","Female","Female","Male","Male", "Female","Male","Male","Male", "Male"),
Est=c(0.9956, 1.035, 2.0731, 0.0824, 3.0987, 5.0982, 6.0707, 5.0393, 2.7046, 4.0783, 0, 2.0923, 4.2348, 1.9561, 6.9262))
我需要再添加一列以将 Est 值重新编码为:
Gender Est X
1 Male 0.9956 0
2 Male 1.0350 1
3 Female 2.0731 2
4 Male 0.9824 0
5 Female 3.0987 3
6 Male 5.0982 5
7 Female 6.0707 6
8 Female 5.0393 5
9 Male 2.7046 2
10 Male 4.0783 4
11 Female 0.0000 0
12 Male 2.0923 2
13 Male 4.2348 4
14 Male 1.9561 1
15 Male 6.9262 6
其中 (0,1]=0, (1,2]=1, (2,3]=2, (3,4]=3, (4,5]=4, (5,6 ]=5, (6,7]=6.
cut divides the range of x into intervals and codes the values in x according to which interval they fall.
正如@alistaire 提到的 cut 的输出是一个因素。因此,您可能希望使用 as.numeric(as.character(cut(try$X ....)))
将其转换回数字
try$X = cut(x = try$Est,breaks = c(0,1,2,3,4,5,6,7),labels = c(0,1,2,3,4,5,6),right = TRUE,include.lowest = TRUE)
我的数据是:
try<-data.frame(Gender=c("Male","Male","Female","Male","Female","Male","Female","Female","Male","Male", "Female","Male","Male","Male", "Male"),
Est=c(0.9956, 1.035, 2.0731, 0.0824, 3.0987, 5.0982, 6.0707, 5.0393, 2.7046, 4.0783, 0, 2.0923, 4.2348, 1.9561, 6.9262))
我需要再添加一列以将 Est 值重新编码为:
Gender Est X
1 Male 0.9956 0
2 Male 1.0350 1
3 Female 2.0731 2
4 Male 0.9824 0
5 Female 3.0987 3
6 Male 5.0982 5
7 Female 6.0707 6
8 Female 5.0393 5
9 Male 2.7046 2
10 Male 4.0783 4
11 Female 0.0000 0
12 Male 2.0923 2
13 Male 4.2348 4
14 Male 1.9561 1
15 Male 6.9262 6
其中 (0,1]=0, (1,2]=1, (2,3]=2, (3,4]=3, (4,5]=4, (5,6 ]=5, (6,7]=6.
cut divides the range of x into intervals and codes the values in x according to which interval they fall.
正如@alistaire 提到的 cut 的输出是一个因素。因此,您可能希望使用 as.numeric(as.character(cut(try$X ....)))
将其转换回数字try$X = cut(x = try$Est,breaks = c(0,1,2,3,4,5,6,7),labels = c(0,1,2,3,4,5,6),right = TRUE,include.lowest = TRUE)