简体   繁体   English

向量等于R的情况

[英]CASE WHEN equivalent in R for a vector

I have a matrix with two columns, the first of which is sometimes NA, and would like to create a third column that is the value of the first, unless it is NA, in which case it takes the value of the second. 我有一个包含两列的矩阵,其中第一列有时是NA,并且想创建第三列,即第一列的值,除非它是NA,在这种情况下它将采用第二列的值。 I have a for loop so far, but I'm sure there's a MUCH better way to do this in R. 到目前为止,我有一个for循环,但是我敢肯定,在R中有一种更好的方法。

matrixA$Age3 <- 1:length(matrixA$Age)
for(i in 1:length(matrixA$Age3))
{
  if(!is.na(matrixA$Age[i]))
  {
    matrixA$Age3[i] = matrixA$Age[i]
  }else
  {
    matrixA$Age3[i] = matrixA$Age2[i]
  }
}
matrix$Age3 <- ifelse(!is.na(matrix$Age),matrix$Age,matrix$Age2)

maybe , just for fun, 也许只是为了好玩,

matrix$Age3 <- sapply(1:nrow(matrix), function(j) matrix[j,(2-!is.na(foo[j,1]))])

(With apologies to CSGillespie if he had anything like this, since he deleted his) (对CSGillespie有这样的事情深表歉意,因为他删除了自己的名字)

EDIT: As eddi properly suggested, here is some expansion and testing of my ideas. 编辑:正如eddi正确建议的那样,这是对我的想法的一些扩展和测试。 I am now properly and publicly shamed for my incorrect assumption that "ifelse" was a timepig. 我现在错误地认为“ ifelse”是一个时间猪,对此我感到公开和羞耻。 switch is easier to read, but at least for this small dataset and limited set of switches, the time difference is not significant. switch更容易阅读,但是至少对于这个小的数据集和有限的开关集,时间差并不明显。 (I may well have fubared the exact cutoff values but the effective operation of these two functions is what matters here). (我可能已经取消了确切的截止值,但是这两个函数的有效操作在这里很重要)。

# foo is a 2e4 row by 5 column matrix of runif values 

ifelse4 <- function(foo) ifelse(foo[,1] > 0.8,foo[,2],ifelse(foo[,1] > 0.6,foo[,3],ifelse(foo[,1] > .4 , foo[,4],ifelse(foo[,1] > .2 , foo[,5], foo[,1])))) 

switch4l <- function(foo) {

 for(j in 1:nrow(foo)) {
         switch( ceiling(foo[j,1]*5),
            foo[j,1],
            foo[j,5],
            foo[j,4],
            foo[j,3],
            foo[j,2] )
        }
        }


    microbenchmark(ifelse4(foo),switch4l(foo),times=10)

    Unit: milliseconds
          expr      min       lq   median       uq      max neval
  ifelse4(foo) 31.37346 31.87336 32.21567 32.44509 33.21182    10
 switch4l(foo) 28.03629 28.31339 28.61871 28.99588 29.78014    10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM