Skip to content
Prev 318308 / 398503 Next

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: