【问题标题】:Replace Zero by value of another variable [duplicate]用另一个变量的值替换零[重复]
【发布时间】:2019-02-08 05:54:19
【问题描述】:

此帖与此帖相似 Replace NA in column with value in adjacent column 但现在如果 x6=0,则必须按 x5 的值返回。 如果我这样做了

mydat$X6[0(mydat$X6)] <- mydat$X5[0(mydat$X6)]

当然我有这个错误:attempt to apply non-function

 mydat=structure(list(ItemRelation = c(158200L, 158204L), DocumentNum = c(1715L, 
                                                                         1715L), CalendarYear = c(2018L, 2018L), X1 = c(0L, 0L), X2 = c(0L, 
                                                                                                                                        0L), X3 = c(0L, 0L), X4 = c(NA, NA), X5 = c(107L, 105L), X6 = c(0, 
                                                                                                                                                                                                        0)), .Names = c("ItemRelation", "DocumentNum", "CalendarYear", 
                                                                                                                                                                                                                         "X1", "X2", "X3", "X4", "X5", "X6"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                  -2L))

如何在 x5 值上用 x6 替换零以获得可嘲笑的输出

  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5 X6
1       158200        1715         2018  0  0  0 NA 107 107
2       158204        1715         2018  0  0  0 NA 105 105

【问题讨论】:

    标签: r dplyr data.table plyr


    【解决方案1】:

    创建一个逻辑 vector 并使用它对替换列和被替换列进行子集化,以在执行赋值操作时使长度相等

    i1 <- mydat$X6 == 0
    mydat$X6[i1] <- mydat$X5[i1]
    

    0(mydat$X6) 语法不清楚 - 可能是伪函数的表示

    【讨论】:

      【解决方案2】:

      你也可以使用replace,即

      mydat$X6 <- with(mydat, replace(X6, X6 == 0, X5[X6 == 0]))
      
      #  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5  X6
      #1       158200        1715         2018  0  0  0 NA 107 107
      #2       158204        1715         2018  0  0  0 NA 105 105
      

      【讨论】:

        【解决方案3】:

        您可以使用?ifelse

        mydat$X6 <- ifelse(mydat$X6 == 0, mydat$X5, mydat$X6)
        
        #  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5  X6
        #1       158200        1715         2018  0  0  0 NA 107 107
        #2       158204        1715         2018  0  0  0 NA 105 105
        

        查看更大数据集的基准。 Ifelse 的执行速度似乎比其他 2 慢。

        mydat <- data.frame(X6=1:999999,X5=sample(0:1,999999,replace = T))
        
        akrun <- function(mydat) {
            i1 <- mydat$X6 == 0
        mydat$X6[i1] <- mydat$X5[i1]
        }
        
        sotos <- function(mydat) {
            mydat$X6 <- with(mydat, replace(X6, X6 == 0, X5[X6 == 0]))
        }
        
        elrico <- function(mydat) {
            mydat$X6 <- ifelse(mydat$X6 == 0, mydat$X5, mydat$X6)
        }
        
        microbenchmark::microbenchmark(elrico(mydat),akrun(mydat),sotos(mydat), times = 100)
        
        #Unit: milliseconds
        #          expr       min        lq      mean    median        uq      max neval cld
        # elrico(mydat) 42.809477 47.591964 56.814627 49.750948 51.972969 148.7152   100   c
        #  akrun(mydat)  5.068961  5.206103  8.277144  5.399385  9.516853 106.4254   100 a  
        #  sotos(mydat)  7.966428  8.199167 16.903062 11.996958 13.774511 110.4206   100  b 
        

        因此,如果您需要速度并使用较大的数据集,请采用 akrun 或 sotos 解决方案。否则,您可以选择我的,这是 IMO 语法上最“美丽”的。

        【讨论】:

          猜你喜欢
          • 2019-02-05
          • 1970-01-01
          • 2019-09-01
          • 2013-08-06
          • 2018-07-28
          • 1970-01-01
          • 1970-01-01
          • 2021-02-22
          • 1970-01-01
          相关资源
          最近更新 更多