简体   繁体   English

用R重新编码缺少数据的项目

[英]Recoding in R for items with missing data

I just started working with R and this is one of problems: I want to recode text variables from one item (column) in numeric variables. 我刚开始使用R,这是问题之一:我想从数字变量中的一项(列)中重新编码文本变量。 Since I had missing values, I tried to recode like this: 由于缺少值,因此我尝试像这样重新编码:

install.packages("dplyr")
library("dplyr")
zdf_all <- read.csv("Daten_einlesen.csv")
zdf2 <- read.csv("Daten_einlesen.csv", header=T, na.strings=c("","NA"))
zdf <- filter(zdf2, Status == "Complete")

names(zdf) [295] <- "pbc"
pbc = recode(zdf$value, 'Definitely agree'=5, 'Somewhat agree'=4, 'Neither agree or disagree'=3,'Somewhat disagree'=2, 'Definitely disagree'=1, 'NA'=0, as.factor.result=FALSE)

When I run the command I get this warning message: 当我运行命令时,得到以下警告消息:

  Error in UseMethod("recode") : 
  no applicable method for 'recode' applied to an object of class "NULL"

Why does it say I have an object of class of "NULL"? 为什么说我有“ NULL”类的对象? How can I recode my items succesfully? 如何成功重新编码商品?

I also tried the ifelse function which didn't work as well. 我还尝试了ifelse函数,该函数也无法正常运行。

install.packages("car")
car::recode(zdf$pbc, "'Definitely agree'=5; 'Somewhat agree'=4; 'Neither agree nor disagree'=3; 'Somewhat disagree'=2; 'Definitely disagree'=1; 'NA'=0")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM