0

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.

Papea
  • 15
  • 1
  • 1
  • 7
  • 1
    Could you share some details about how does the data frame `zdf` look? like output of `str(zdf)` – Gaurav Taneja Feb 13 '18 at 10:34
  • Why would you recode `NA` to `0` when converting a character scale to numeric? – LAP Feb 13 '18 at 10:42
  • @Taneja: do you mean this? $ pbc : Factor w/ 5 levels "Definitely agree",..: 3 1 5 1 5 3 4 2 3 3 ... – Papea Feb 13 '18 at 10:46
  • We need example data, please provide by `dput(zdf)` or `dput(head(zdf))` if it's too big. – jay.sf Feb 13 '18 at 10:47
  • @LAP: yes, this doesn't make sense, I recodes 'NA' to '0' in the second step, i thought it might could help – Papea Feb 13 '18 at 10:48
  • @jaySF: i have 637 variables (nominal, ordinal and metric), since this are very sensible data, can you tell me a common way to show here example data while respecting data protection? – Papea Feb 13 '18 at 10:54
  • Ok, in this case you may want to consider [this](https://stackoverflow.com/a/5963610/6574038) – jay.sf Feb 13 '18 at 10:55
  • @Papea, just `sample` from some columns and stitch them together to create a fictional dataframe. Example: `df <- data.frame(Status = sample(zdf2$Status, 10, replace = TRUE), value = sample(zdf2$value, 10, replace = TRUE))`. – LAP Feb 13 '18 at 11:21

1 Answers1

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")
  • Thank so much, it worked! I just had to replace "," by ";". `car::recode(zdf$pbc, "'Definitely agree'=5; 'Somewhat agree'=4; 'Neither agree nor disagree'=3; 'Somewhat disagree'=2; 'Definitely disagree'=1; 'NA'=0")` – Papea Feb 13 '18 at 12:56