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) b) why there is a different behavior from the command line and from a script? c) how to solve the problem? (As the amount of data is large I need to do everything via scripts) Thank you for your attention. -- Saluti, Antonio Prioglio -- We are what we repeatedly do. Excellence, then, is not an act, but a habit. Aristoteles /"\ \ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL X - AGAINST MS ATTACHMENTS / \ http://www.gnu.org/philosophy/no-word-attachments.html
On 07/03/04 16:45, a.prioglio at 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/
Jonathan Baron <baron <at> psych.upenn.edu> writes: : ces$score <- ifelse(ces$question %in% c(1,3,5),5-ces$score,ces$score) One minor improvement in readability might be: ces$score <- with(ces, ifelse(question %in% c(1,3,5), 5-score, score))
On Sat, Jul 03, 2004 at 12:17:11PM -0400, Jonathan Baron wrote:> >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?I guess I discovered what was the issue ( I knew it was something silly) In the script I broke the line in two for easier reading forgetting that the left hand side is a valid statement in itself! This is nasty. Thanks again for your help. -- Saluti, Antonio Prioglio -- We are what we repeatedly do. Excellence, then, is not an act, but a habit. Aristoteles /"\ \ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL X - AGAINST MS ATTACHMENTS / \ http://www.gnu.org/philosophy/no-word-attachments.html