-1

I would like to recode items for a questionnaire. I have a scale from 1 to 5, currently the best value is 5, but I want the best value to be 1.

# loading packages

#install.packages("readxl")
#install.packages("car")

library(readxl)
library(car)


# loading data

data_infranken <- read_excel("H:/Treiberanalyse/infranken_NPS.xlsx")

data_infranken <- str(data_infranken)



# searching for missing data

data_infranken <- is.na(data_infranken)

data_infranken <- sum(is.na(data_infranken)) # 23464


# removig missing data

data_infranken <- na.omit(data_infranken)


# recoding

data_infranken$Aktualität <- recode(data_infranken["Bewerten Sie bitte inFranken.de nach folgenden 
Gesichtspunkten: : Aktualität"], "5=1; 4=2; 3=3; 2=4; 1=5")
data_infranken$Aktualität

data_infranken$Aktualität <- apply(data_infranken["Bewerten Sie bitte inFranken.de nach folgenden 
Gesichtspunkten: : Aktualität"], 2, mean, recode(data_infranken["Bewerten Sie bitte inFranken.de nach 
folgenden Gesichtspunkten: : Aktualität"], "5=1; 4=2; 3=3; 2=4; 1=5")

Error:

Error: unexpected symbol in: "apply(data_infranken["Bewerten Sie bitte inFranken.de nach folgenden Gesichtspunkten: : Aktualität"], 2, mean...

PKumar
  • 10,971
  • 6
  • 37
  • 52
Kitty123
  • 171
  • 2
  • 12
  • 1
    I am not sure , but you can do (6 - df$column), not sure if that solves your purpose but if you just want to invert them , it will do, Here df is your dataframe and column is the column is the column which contains values from 1 to 5 – PKumar Mar 28 '21 at 11:50
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 28 '21 at 13:41

1 Answers1

0

you are pretty close:

# dummy data frame
data_infranken <- data.frame("Bewerten Sie bitte inFranken.de nach folgenden Gesichtspunkten: : Aktualität" = c(1, 2, 3, 4, 5))
# recording
data_infranken$Aktualität <- recode(data_infranken[, 1], "5"="1", "4"="2", "3"="3", "2"="4", "1"="5")

 Bewerten.Sie.bitte.inFranken.de.nach.folgenden.Gesichtspunkten....Aktualität Aktualität
 1                                                                            1          5
 2                                                                            2          4
 3                                                                            3          3
 4                                                                            4          2
 5                                                                            5          1
DPH
  • 4,244
  • 1
  • 8
  • 18
  • thank you. @DPH Your solution worked for my first variable. data_infranken$Aktualität <- data_infranken[, 16] <- recode(data_infranken[, 16], "5=1; 4=2; 3=3; 2=4; 1=5"). But for the second : data_infranken$Relevanz_der_Inhalte <- data_infranken[, 17] <- recode(data_infranken[, 17], "5=1; 4=2; 3=3; 2=4; 1=5") I receive the error: Wrong number of dimensions? How can that be? – Kitty123 Mar 28 '21 at 17:30
  • the call you are showing is assinging two times (2x "<-" ) and it does not match the call from your question... the one you just wrote should work like this: data_infranken[, 17] <- recode(data_infranken[, 17], "5"="1", "4"="2", "3"="3", "2"="4", "1"="5") – DPH Mar 28 '21 at 17:44