当前位置:网站首页>R语言 bug?报错?关于亚变量0、1 结局outcome,outcome变量经过factor和numeric过程,改变了原始内容?

R语言 bug?报错?关于亚变量0、1 结局outcome,outcome变量经过factor和numeric过程,改变了原始内容?

2022-06-21 16:59:00 违规账号247188

问题RT

因为我在做PSM的逆概率加权和匹配法,和做表格时的卡方检验,他们对结局的变量的类型要求是不同的,我在转化过程中,发现outcome内容 0 1变成 12 ,虽然他们是0=1 、1=2的转化影响不大,但是我在分层提取 outcome=0时,想提取对照组,发现没有对应的数据。所以这个bug是如何产生的?

data <- iris #鸢尾花
data$outcome <- rep(0:1,each=75)
table(data$outcome)
str(data$outcome)
#outcome因子化
data$outcome <- as.factor(data$outcome)  
table(data$outcome) #此时outcome还是 01
str(data$outcome)
#outcome继续转为数值型
data$outcome <- as.numeric(data$outcome)
table(data$outcome)  #这时候发现outcome  01亚变量变成了12
str(data$outcome)
#【问题,outcome变量经过factor和numeric过程,改变了原始内容?

在这里插入图片描述

原网站

版权声明
本文为[违规账号247188]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46623488/article/details/121002386