根据值绑定新列

时间:2018-08-06 03:27:02

标签: r cbind recode

我的数据是:

 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。

1 个答案:

答案 0 :(得分:0)

  

cut将x的范围划分为间隔,并根据x的值落入的间隔对值进行编码。

正如@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)