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

时间:2018-02-13 10:32:09

标签: r null recode

我刚刚开始使用R,这是一个问题: 我想从数字变量中的一个项目(列)重新编码文本变量。由于我缺少值,我试图像这样重新编码:

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)

当我运行命令时,我收到此警告消息:

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

为什么说我有一个“NULL”类的对象?如何成功重新编码我的物品?

我也尝试了ifelse功能,但效果不佳。

1 个答案:

答案 0 :(得分:0)

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")