Re: [R] Recoding scores of negatively worded item

From: Jonathan Baron <baron_at_psych.upenn.edu>
Date: Sun 04 Jul 2004 - 02:17:11 EST

On 07/03/04 16:45, a.prioglio@city.ac.uk wrote:
>Hi,
>
>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 -
>ces[is.element(ces$question,c(1,3,5),]$score
>
>the system correctly recode the scores for questions 1,3,5 on the table
>ces.
>
>If the same expression is processed as part of a script I get a "syntax
>error".
>
>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%  c(1,3,5),]$score

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
>script?

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

Jon

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sun Jul 04 02:20:50 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:27:39 EST