嵌套的ifelse()是最糟糕的;什么是最好的? [英] nested ifelse() is the worst; what's the best?

查看:99
本文介绍了嵌套的ifelse()是最糟糕的;什么是最好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这是对如何在R <中高效实现合并的愚蠢/ a>,同意。我没有意识到我的问题比我的具体应用更普遍,所以这个讨论很棒。

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.

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

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)


推荐答案

我喜欢我们ea coalesce()此功能

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]])
}

然后你可以做

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

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

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

这篇关于嵌套的ifelse()是最糟糕的;什么是最好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆