lundi 7 novembre 2016

A count variable function in R

I am trying to create a new index variable from 0-100% from multiple survey items (q1:q15) with a 7pt. response scale. I found an example in Excel that will accomplish this using the Count function but I would like to automate this with R. There are missing data for some of the items.

I also need to specify the following criteria: At least one item from each of the following groups of items (q1:q3,q4:q6,q7:q9, q10:q12) must have data or the Index should result in NA.

structure(list(q1 = c(6L, 5L, 2L, 7L), q2 = c(5L, 5L, 6L, 7L), 
q3 = c(4L, NA, 2L, 7L), q4 = c(NA, 5L, 5L, 5L), q5 = c(NA, 
6L, 6L, 6L), q6 = c(NA, 6L, 2L, 7L), q7 = c(6L, 7L, 7L, 7L
), q8 = c(5L, 6L, NA, 7L), q9 = c(6L, 5L, 7L, 7L), q10 = c(7L, 
NA, 5L, 7L), q11 = c(6L, 5L, 7L, 7L), q12 = c(6L, 6L, 2L, 
7L), q13 = c(6L, 5L, 6L, 7L), q14 = c(4L, 4L, 1L, 7L), q15 = c(6L, 
7L, 4L, 7L)), .Names = c("q1", "q2", "q3", "q4", "q5", "q6", 
"q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"), class = "data.frame", row.names = c(NA, 
-4L))

The coding works in Excel (except for the criteria about non-missing data in the grops of items):

=(SUM(q1:q15)-COUNT(q1:q15))/((COUNT(q1:q15)*7)-COUNT(q1:q15))*100

Thanks for assistance.

Aucun commentaire:

Enregistrer un commentaire