[R] Recoding scores of negatively worded item

Jonathan Baron baron at psych.upenn.edu
Sat Jul 3 18:17:11 CEST 2004

On 07/03/04 16:45, a.prioglio at city.ac.uk wrote:
>I'm new to R so please fogive if I write someting silly ...
>I need to recode a series of responses from a number of questionnaires.
>The data is read via ODBC from a database where all responses are coded
>as tables of the form (id, question, score).
>After dealing with recoding of missing values, I need to "invert" the
>scores of some questionnaire's item in the form x <- (n - x) where n is
>the range of allowed responses + 1, e.g. if score can range from 1 to 4
>n would by 5.
>I am using R version 1.9.1 and 1.8.1 with identical outcome.
>If from the R interpreter I write
>ces[is.element(ces$question,c(1,3,5),]$score <- 5 -
>the system correctly recode the scores for questions 1,3,5 on the table
>If the same expression is processed as part of a script I get a "syntax
>My question is a) is this the best way to recode scores? (I did look at
>the package car but I did not see how to perform a conditional recoding,
>nor was obvious how to to operations on scores)

Other (but similar) ways you might try are:

ces[ces$question %in% c(1,3,5),]$score <- 5-ces[ces$question %in%

ces$score <- ifelse(ces$question %in% c(1,3,5),5-ces$score,ces$score)

>b) why there is a different behavior from the command line and from a

Could the problem be on the line before the one that gives the error?

Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page:            http://www.sas.upenn.edu/~baron
R search page:        http://finzi.psych.upenn.edu/

More information about the R-help mailing list