afei2017 A=factor(rep(c(1:3),each=7)) A [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 Levels: 1 2 3 A[9] [1] 2 Levels: 1 2 3 我想获取A[9]对应的那个值2,怎么获取?谢谢
liamml tctcab 为什么。我的意思是如果1对应的level是3.直接as.numeric()就会1变成3 > x <- factor(c(1,2,3),levels = c(3,2,1)) > x [1] 1 2 3 Levels: 3 2 1 > as.numeric(x) [1] 3 2 1
tctcab liamml 是啊,可是factor不一定是数字编码,更常见的是字符串,这个时候as.numeric(as.character(x))就会报错,我是这个意思。 x = factor(c("a","b","c")) as.numeric(as.character(x)) #> Warning: NAs introduced by coercion #> [1] NA NA NA <sup>Created on 2018-10-29 by the reprex package (v0.2.1)</sup>