jeudi 1 juin 2017

Count combinations in indicators

I am trying to figure out a more efficient way of calculating the number of correct combinations within the indicators.

Here is my data:

head(data)
   email_flag home_number_flag mobile_flag
1:  incorrect        incorrect     correct
2:  incorrect        incorrect   incorrect
3:  incorrect        incorrect   incorrect
4:  incorrect        incorrect   incorrect
5:  incorrect        incorrect   incorrect
6:  incorrect        incorrect   incorrect

My current approach with an ifelse statement:

data <- mutate(data, number_of_correct_flags =
    +                            ifelse(email_flag == "correct" & mobile_flag == "correct", 2, 
    +                            ifelse(email_flag != "correct" & mobile_flag == "correct", 1, 
    +                            ifelse(email_flag == "correct" & mobile_flag != "correct", 1,
    +                            ifelse(email_flag != "correct" & mobile_flag != "correct", 0,
    +                                                        
    +                            ifelse(home_number_flag == "correct" & mobile_flag == "correct", 2, 
    +                            ifelse(home_number_flag != "correct" & mobile_flag == "correct", 1, 
    +                            ifelse(home_number_flag == "correct" & mobile_flag != "correct", 1,
    +                            ifelse(home_number_flag != "correct" & mobile_flag != "correct", 0, 
    +                                                                                    
    +                            ifelse(email_flag == "correct" & mobile_flag == "correct", 2, 
    +                            ifelse(email_flag != "correct" & mobile_flag == "correct", 1, 
    +                            ifelse(email_flag == "correct" & mobile_flag != "correct", 1,
    +                            ifelse(email_flag != "correct" & mobile_flag != "correct", 0, 
    +                                   
    +                            ifelse(email_flag == "correct" & mobile_flag == "correct" & home_number_flag == "correct", 3, 
    +                            ifelse(email_flag != "correct" & mobile_flag != "correct" & home_number_flag != "correct", 0, "check")))))))))))))))

Result:

head(data)
      email_flag home_number_flag mobile_flag number_of_correct_flags
    1  incorrect        incorrect     correct                       1
    2  incorrect        incorrect   incorrect                       0
    3  incorrect        incorrect   incorrect                       0
    4  incorrect        incorrect   incorrect                       0
    5  incorrect        incorrect   incorrect                       0
    6  incorrect        incorrect   incorrect                       0

Obviouslly, this becomes problematic as the number of indicators grows.

Any thoughts on a more efficient approach?

Aucun commentaire:

Enregistrer un commentaire