嵌套的ifelse()是最糟糕的;什么是最好的?

时间:2015-05-06 22:39:00

标签: r if-statement

编辑:这是How to implement coalesce efficiently in R的欺骗,同意。我没有意识到我的问题比我的具体应用更普遍,所以这个讨论很棒。

有时,随机实验中的响应变量包含在每个实验组的不同列中(下面的代码中为Y_1到Y_5)。通常最好将响应变量收集到一个列(Y_all)中。我最终这样做,如下例所示。但我确定那里有更好的方法。想法?

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)

3 个答案:

答案 0 :(得分:5)

我喜欢为此

使用coalesce()函数
#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]])
}

然后你可以做

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

当然,这非常特定于获得第一个非NA值。

答案 1 :(得分:2)

我认为在这种情况下你可以使用融合函数将数据转换为长格式然后去掉缺失的值:

library(reshape2)

set.seed(10)
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)

Y_all = data.frame(group, Y_1, Y_2,Y_3,Y_4,Y_5)

Y_all.m = melt(Y_all, id.var="group")
Y_all.m = Y_all.m[!is.na(Y_all.m$value),]

答案 2 :(得分:1)

将矢量存储在矩阵中,然后选择:

Ymat    <- cbind(Y_1,Y_2,Y_3,Y_4,Y_5)
mycol   <- apply(!is.na(Ymat),1,which)

Y_all.f <- Ymat[cbind(1:nrow(Ymat),mycol)]

identical(Y_all,Y_all.f) # TRUE