0

I am trying to view how many missing I have per questionnaires for a specific group of participants. i.e.

I have a dataframe i.e.

id Result QA1 QA2 QA3 QA4 QA5 QA6 QB1 QB2 QB3 QB4 QB5 QB6
1   1      1   3    2   2  3   3   3  NA    1   1  2   1
2   1      2   NA    2   2  2  1   1   3    2   1  2   3 
3   2      3   2    3   1  1   1   2   1    1   NA 3  NA 
4   1      2   1    NA  3  2   NA  1   3    3   1  2   1 
5   6      1   1    3   2  1   3   2   1    1   1  1  NA 

Say I want to know how many missing there are in questionnaire A for all results that are coded by 1, how can I do this? Any suggestions?

ekad
  • 14,436
  • 26
  • 44
  • 46
Z.Chanell
  • 35
  • 8
  • Please provide reproducible example. You can use `dput()` to get it – Sotos Aug 23 '16 at 12:52
  • Something like ``sum(is.na(yourdataframeorcolumn))`` can help. – Horst Grünbusch Aug 23 '16 at 13:00
  • Yes, however this only gives me the missing for that column. What I want is the missing for 28 different columns given a specific result (result is also a column) so I want the missings for say column 1:28 given that column 29 = 1 – Z.Chanell Aug 23 '16 at 13:08
  • @Sotos I can't provide the data I am working with that's why I only put the column names as an example – Z.Chanell Aug 23 '16 at 13:09
  • You can create sample dummy data. In general have in mind that the best way to get help here in SO, is to include reproducible example and expected output. [Have a look at this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Sotos Aug 23 '16 at 13:10
  • I updated my question. Thanks! – Z.Chanell Aug 23 '16 at 13:39

1 Answers1

1

You can create a function which takes as arguments the dataframe, the questionnaire and the code, i.e.

fun1 <- function(df, questionnaire, code){
  d <- sum(is.na(df[df$Result == code,grepl(questionnaire, names(df))]))
  return(d)
} 
fun1(df, 'A', 1)
#[1] 3

fun1(df, 'B', 1)
#[1] 1

fun1(df, 'A', 2)
#[1] 0
Sotos
  • 51,121
  • 6
  • 32
  • 66