mercredi 6 mai 2015

nested ifelse() is the worst; what's the best?

Sometimes, the response variable in a randomized experiment is contained in a different column for each experimental group (Y_1 through Y_5 in the code below). It's often best to collect the response variable into a single column (Y_all). I end up doing it as in the example below. But I'm SURE there's a better way. thoughts?

N <- 1000
group <- sample(1:5, N, replace=TRUE)
Y_1 <- ifelse(group==1, rbinom(sum(group==1), 1, .5), NA)
Y_2 <- ifelse(group==2, rbinom(sum(group==2), 1, .5), NA)
Y_3 <- ifelse(group==3, rbinom(sum(group==3), 1, .5), NA)
Y_4 <- ifelse(group==4, rbinom(sum(group==4), 1, .5), NA)
Y_5 <- ifelse(group==5, rbinom(sum(group==5), 1, .5), NA)

## This is the part I want to make more efficient
Y_all <- ifelse(!is.na(Y_1), Y_1, 
                ifelse(!is.na(Y_2), Y_2, 
                       ifelse(!is.na(Y_3), Y_3, 
                              ifelse(!is.na(Y_4), Y_4, 
                                     ifelse(!is.na(Y_5), Y_5, 
                                            NA)))))

table(Y_all, Y_1, exclude = NULL)
table(Y_all, Y_2, exclude = NULL)

Aucun commentaire:

Enregistrer un commentaire