我有一个数据集,其列的值为Yes和No,并且它们以非常规的2或3编码。我猜是NO是较低的数字2,而YES是较大的数字3,但是我不能确定。
有没有办法发现哪个被编码为哪个?
str(df$col)
告诉我
Factor w/2 levels 'no','yes': 3,2,3,2,3, etc and I can't tell from this.
查看R中的实际文件显示是和否,而不是数字。
有没有让我解码的命令?
答案 0 :(得分:1)
我相信情况都不是 - “不”是1,“是”是2,3是NA:
> col = structure(c(3, 2, 3, 2, 3), .Label = c("no", "yes"), class = "factor")
> col
[1] <NA> yes <NA> yes <NA>
Levels: no yes
> str(col)
Factor w/ 2 levels "no","yes": 3 2 3 2 3
答案 1 :(得分:0)
levels(df$col)
应该做你需要的,我想。
答案 2 :(得分:0)
可以创建一些愚蠢的效用函数,如下所示
factfun <- function(x){
unique(data.frame(Level = as.character(x), Encoding = as.numeric(x)))
}
用法
factfun(df$col)
测试@SenorsO数据
col = structure(c(3, 2, 3, 2, 3), .Label = c("no", "yes"), class = "factor")
factfun(col)
## Level Encoding
## 1 <NA> 3
## 2 yes 2