Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

if-statement

EDIT: this is a dupe of How to implement coalesce efficiently in R, agreed. I didn't realize how my problem was more general than my specific application, so this discussion has been great.

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?

set.seed(343)
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)
like image 707
Alex Coppock Avatar asked May 06 '15 22:05

Alex Coppock


1 Answers

I like to use a coalesce() function for this

#available from https://gist.github.com/MrFlick/10205794
coalesce<-function(...) {
    x<-lapply(list(...), function(z) {if (is.factor(z)) as.character(z) else z})
    m<-is.na(x[[1]])
    i<-2
    while(any(m) & i<=length(x)) {
        if ( length(x[[i]])==length(x[[1]])) {
            x[[1]][m]<-x[[i]][m]
        } else if (length(x[[i]])==1) {
            x[[1]][m]<-x[[i]]
        } else {
            stop(paste("length mismatch in argument",i," - found:", length( x[[i]] ),"expected:",length( x[[1]] ) ))
        }
        m<-is.na(x[[1]])
        i<-i+1
    }
    return(x[[1]])
}

Then you can do

Y_all <- coalesce(Y_1,Y_2,Y_3,Y_4,Y_5)

Of course, this is very specific to getting the first non-NA value.

like image 177
MrFlick Avatar answered Sep 28 '22 15:09

MrFlick