如何查看R中编码的因子变量
我有一个数据集,其中一列的值为Yes和No,它们被编码为非常规的2或3。我猜NO是较小的数字2,YES是较大的数字3,但我不能确定 有没有办法发现哪个编码为哪个如何查看R中编码的因子变量,r,R,我有一个数据集,其中一列的值为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中查看实际文件会显示是和否,而不是数字 有让我解码的命令吗 级别(df$col)应该满足您的需要。可以创建一些愚蠢的实用功能,如下所示 factfun
str(df$col)
给我看
Factor w/2 levels 'no','yes': 3,2,3,2,3, etc and I can't tell from this.
在R中查看实际文件会显示是和否,而不是数字
有让我解码的命令吗
级别(df$col)
应该满足您的需要。可以创建一些愚蠢的实用功能,如下所示
factfun <- function(x){
unique(data.frame(Level = as.character(x), Encoding = as.numeric(x)))
}
测试@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
col=structure(c(3,2,3,2,3),.Label=c(“否”、“是”),class=“因子”)
factfun(col)
##电平编码
## 1 3
##2是的2
我认为两者都不是-“否”是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
>col=structure(c(3,2,3,2,3),.Label=c(“否”,“是”),class=“因子”)
>上校
[1] 对对
级别:否是
>str(col)
系数w/2级“否”、“是”:3
dput(head(df$col))返回什么?奇怪的是,这不是1/2。而不是2/3。我真的很惊讶一个有2个级别的因子里面有一个3。。。这应该会产生NA
在df$col
中是否有NAs?检查sum(is.na(df$col))
> 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