If you do this sort of thing a lot you may find the psych package helpful: # make example data x <- 1:3 dat <- data.frame(expand.grid(Item1=x, Item2=x, Item3=x, Item4=x))
# make scoring ky key <- c(Item1=1, Item2=2, Item3=1, Item4=1) # load psych library library(psych) # score (scores <- score.multiple.choice(key, dat, total=TRUE, short=FALSE)) dat$Variable <- scores$scores Best, Ista On Mon, Feb 25, 2013 at 8:02 PM, Craig J <cjohn...@gmail.com> wrote: > I'm learning R and am converting some code from SPSS into R. My background > is in SAS/SPSS so the vectorization is new to me and I'm trying to learn > how to NOT use loops...or use them sparingly. I'm wondering what the > most efficient to tackle a problem I'm working on is. Below is an example > piece of code. Essentially what it does is set a variable to zero, loop > through item responses, and add one if a condition is met. For example, if > item one was responded as a 1 then add one to the final variable. If item > two was responded as a 2 then add one to the final variable. I have to do > this for five scales with each scale having 6 items half of which may have > the 1 response pattern and half the 2 pattern. > > Any suggestions on how best to tackle this in R would be helpful. > > Craig > > ********** > Old SPSS code sample > ********** > > Compute Variable = 0. > > IF (item1 = 1) Variable = Variable +1 . > > IF (item2= 2) Variable = Variable +1 . > > IF (item3 = 1) Variable = Variable +1. > > IF (item4 = 2) Variable = Variable +1. > > EXECUTE . > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.