将因子重新编码为我选择的数量

时间:2018-03-08 22:51:28

标签: r sapply

我喜欢将NG转换为0,SG = 1.25,LG = 7.25,MG = 26且HG = 40

我的实际数据与下面的t完全相同,如下所示: actual data causing problems

t<-rep(c("NG","SG","LG","MG","HG"),each=5)
colnames(t)<-c("X.1","X1","X2","X4","X8","X12","X24","X48")

为什么这不起作用?

t[t=="NG"] <- "0"
t[t=="SG"] <- "1.25"
t[t=="LG"] <- "7.25"
t[t=="MG"] <- "26"

或者这个:

factor(t, levels=c("NG","SG","LG","MG", "HG"), labels=c("0","1.25","7.25","26","40"))

或者这个:

t <- sapply(t,switch,"NG"=0,"SG"=1.25,"LG"=7.25,"MG"=26, "HG"=40)

1 个答案:

答案 0 :(得分:1)

你可能想要这个:

t <- rep(c(NG = 0, SG = 1.25, LG = 7.25, MG = 26, HG = 40), each = 5)
t <- factor(t)
levels(t)
# [1] "0"    "1.25" "7.25" "26"   "40"  
labels(t)
# [1] "NG" "NG" "NG" "NG" "NG" "SG" "SG" "SG" "SG" "SG" "LG" "LG" "LG" "LG" "LG"
# [16] "MG" "MG" "MG" "MG" "MG" "HG" "HG" "HG" "HG" "HG"

因子的内部代码将始终为整数,因此您无法创建具有双精度浮点数的内部代码的因子。

unclass(t)
# NG NG NG NG NG SG SG SG SG SG LG LG LG LG LG MG MG MG MG MG HG HG HG HG HG 
#  1  1  1  1  1  2  2  2  2  2  3  3  3  3  3  4  4  4  4  4  5  5  5  5  5 
# attr(,"levels")
# [1] "0"    "1.25" "7.25" "26"   "40"  

您仍然可以使用级别标签提取数值:

t["SG"]
#   SG
# 1.25
# Levels: 0  1.25  7.25  26  40