An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130225/3b098f77/attachment.pl>
Converting code to R Question
4 messages · Craig J, Jeremy Miles, William Dunlap +1 more
Here's a direct translation: Variable <- 0 Variable <- ifelse(item1 == 1, Variable +1, Variable) Variable <- ifelse(item2 == 1, Variable +1, Variable) Variable <- ifelse(item3 == 1, Variable +1, Variable) Variable <- ifelse(item4 == 1, Variable +1, Variable) Here's another way to do it: Variable <- 0 + (item1 == 1) + (item2 == 1) + (item3 == 1) + (item4 == 1) Note that I haven't worried about missing data - do you have NAs in your items? If you do, and you want NA to be not equal to 1 (rather than equal to NA): Variable <- sum((item1 == 1), (item2 == 1) , (item3 == 1) , (item4 == 1), na.rm=TRUE) Jeremy
On 25 February 2013 17:02, Craig J <cjohns38 at 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 at 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.
Note that in Variable <- 0 + (item1 == 1) + (item2 == 1) + (item3 == 1) + (item4 == 1) the '0 +' is not needed, since adding logicals produces integers. Your second solution Variable <- sum((item1 == 1), (item2 == 1) , (item3 == 1) , (item4 == 1), na.rm=TRUE) gives the wrong answer, since sum() always returns a scalar. You could treat logical NA's as FALSE's by defining is.true <- function(x) !is.na(x) & x and using it as Variable <- is.true(item1==1) + is.true(item2==1) + is.true(item3==1) + is.true(item4==1) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeremy Miles Sent: Monday, February 25, 2013 5:41 PM To: Craig J Cc: r-help Subject: Re: [R] Converting code to R Question Here's a direct translation: Variable <- 0 Variable <- ifelse(item1 == 1, Variable +1, Variable) Variable <- ifelse(item2 == 1, Variable +1, Variable) Variable <- ifelse(item3 == 1, Variable +1, Variable) Variable <- ifelse(item4 == 1, Variable +1, Variable) Here's another way to do it: Variable <- 0 + (item1 == 1) + (item2 == 1) + (item3 == 1) + (item4 == 1) Note that I haven't worried about missing data - do you have NAs in your items? If you do, and you want NA to be not equal to 1 (rather than equal to NA): Variable <- sum((item1 == 1), (item2 == 1) , (item3 == 1) , (item4 == 1), na.rm=TRUE) Jeremy On 25 February 2013 17:02, Craig J <cjohns38 at 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 at 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 at 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.
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 <cjohns38 at 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 at 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.