-1

adding reproducible code as suggested by answers

Qs<-paste0("Q2_", 1:18)

set.seed(15)
maindata <- data.frame(ID=1:5)
for(q in Qs) {
    maindata[,q] <- sample(1:20,5,replace=T)
} 

I have below code. Is their a better to achieve the output without writing each line? If i thought of writing the for loop for iterating over questions 1 to 18 but felt that for loop might not be too efficient...

  ifelse(maindata$Q2_1  > 2 & maindata$Q2_1< 11 & !is.na(maindata$Q2_1), 1, 0 )+
  ifelse(maindata$Q2_2  > 2 & maindata$Q2_2< 11 & !is.na(maindata$Q2_2), 1, 0)+
  ifelse(maindata$Q2_3  > 2 & maindata$Q2_3< 11 & !is.na(maindata$Q2_3), 1, 0)+
  ifelse(maindata$Q2_4  > 2 & maindata$Q2_4< 11 & !is.na(maindata$Q2_4), 1, 0)+
  ifelse(maindata$Q2_5  > 2 & maindata$Q2_5< 11 & !is.na(maindata$Q2_5), 1, 0)+
  ifelse(maindata$Q2_6  > 2 & maindata$Q2_6< 11 & !is.na(maindata$Q2_6), 1, 0)+
  ifelse(maindata$Q2_7  > 2 & maindata$Q2_7< 11 & !is.na(maindata$Q2_7), 1, 0)+
  ifelse(maindata$Q2_8  > 2 & maindata$Q2_8< 11 & !is.na(maindata$Q2_8), 1, 0)+
  ifelse(maindata$Q2_9  > 2 & maindata$Q2_9< 11 & !is.na(maindata$Q2_9), 1, 0)+
  ifelse(maindata$Q2_10  > 2 & maindata$Q2_10< 11 & !is.na(maindata$Q2_10), 1, 0)+
  ifelse(maindata$Q2_11  > 2 & maindata$Q2_11< 11 & !is.na(maindata$Q2_11), 1, 0)+
  ifelse(maindata$Q2_12  > 2 & maindata$Q2_12< 11 & !is.na(maindata$Q2_12), 1, 0)+
  ifelse(maindata$Q2_13  > 2 & maindata$Q2_13< 11 & !is.na(maindata$Q2_13), 1, 0)+
  ifelse(maindata$Q2_14  > 2 & maindata$Q2_14< 11 & !is.na(maindata$Q2_14), 1, 0)+
  ifelse(maindata$Q2_15  > 2 & maindata$Q2_15< 11 & !is.na(maindata$Q2_15), 1, 0)+
  ifelse(maindata$Q2_16  > 2 & maindata$Q2_16< 11 & !is.na(maindata$Q2_16), 1, 0)+
  ifelse(maindata$Q2_17  > 2 & maindata$Q2_17< 11 & !is.na(maindata$Q2_17), 1, 0)+
  ifelse(maindata$Q2_18  > 2 & maindata$Q2_18< 11 & !is.na(maindata$Q2_18), 1, 0)
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • Three useful readings: `?with`, `?sapply`, and `?ifelse` – Rich Scriven Jul 01 '14 at 03:07
  • Generally it's helpful to post a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can test and compare different solutions – MrFlick Jul 01 '14 at 03:26

1 Answers1

1

Well, here's one way. First, let's create some sample data

Qs<-paste0("Q2_", 1:18)

set.seed(15)
maindata <- data.frame(ID=1:5)
for(q in Qs) {
    maindata[,q] <- sample(1:20,5,replace=T)
}

Here we make a list of all the question names (Qs) and we create a data.frame with 5 rows where each column contains values sampled from 1:20. If we want the score for each line for each individual, we can do

score <- rowSums(sapply(Qs, function(q) 
    maindata[,q] > 2 & maindata[,q] <11 & !is.na(maindata[,q]) )+0)

Here I use sapply to iterate over the question names. Then i wrote the formula once and swap in the different question names. Here I return a simple logical value and add zero to convert FALSE to 0 and TRUE to 1. Then I use rowSums to app up scores across rows. We can see the results with

cbind(maindata[,"ID", drop=F], score)

#   ID score
# 1  1     9
# 2  2     8
# 3  3     4
# 4  4     6
# 5  5    10
MrFlick
  • 195,160
  • 17
  • 277
  • 295