简体   繁体   中英

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. 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"? How can I recode my items succesfully?

I also tried the ifelse function which didn't work as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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