【问题标题】:Replace consecutive zeros found at either end of a dataframe in R替换在R中数据帧任一端找到的连续零
【发布时间】:2017-05-11 14:09:22
【问题描述】:

我需要用 NA 替换数据帧的第一列和最后一列中的任何零,但是,当替换第一个/最后一个零时,我还需要替换该特定行中存在的任何连续零。给定示例数据框:

df <- data.frame(a = c(1,0,1,0,1,1,1,0,1,1,1),
                 b = c(1,1,1,0,1,1,1,0,1,1,1),
                 c = c(1,0,1,1,1,0,1,0,1,1,1),
                 d = c(1,1,1,0,1,1,1,1,1,1,1),
                 e = c(1,0,1,0,1,1,1,1,1,1,1),
                 f = c(1,1,1,1,1,1,1,1,1,0,1))
df

我需要它返回:

df.result <- data.frame(a = c(1,NA,1,NA,1,1,1,NA,1,1,1),
                        b = c(1,1,1,NA,1,1,1,NA,1,1,1),
                        c = c(1,0,1,1,1,0,1,NA,1,1,1),
                        d = c(1,1,1,0,1,1,1,1,1,1,1),
                        e = c(1,0,1,0,1,1,1,1,1,1,1),
                        f = c(1,1,1,1,1,1,1,1,1,NA,1))
df.result

提前致谢。

【问题讨论】:

    标签: r


    【解决方案1】:

    另一种方式,避免apply并对行进行操作:

    g<-lapply(df,"==",0)
    df[do.call(cbind,Reduce("&",g,accumulate=TRUE)) | do.call(cbind,Reduce("&",g,accumulate=TRUE,right=TRUE))]<-NA
    identical(df,df.result)
    #[1] TRUE
    

    快速基准测试:

    docendo<-function(df) {
      idx <- t(apply(df != 0, 1, function(x) cumsum(x) == 0 | rev(cumsum(rev(x)) == 0)))
      df[idx] <- NA
      df 
    }
    
    nicola<-function(df) {
      g<-lapply(df,"==",0)
      df[do.call(cbind,Reduce("&",g,accumulate=TRUE)) | do.call(cbind,Reduce("&",g,accumulate=TRUE,right=TRUE))]<-NA
      df
    }
    
    lmo<-function(df) {
       reps.first <- max.col(df, ties.method = "first") - 1
       reps.last <- max.col(df, ties.method = "last")
       fill.last <- length(df)-reps.last
       is.na(df[cbind(rep(seq_len(nrow(df))[reps.first > 0], reps.first[reps.first > 0]),
                   sequence(reps.first))]) <- TRUE
       is.na(df[cbind(rep(seq_len(nrow(df))[fill.last > 0], fill.last[fill.last > 0]),
                   length(df)-(sequence(fill.last) - 1))]) <- TRUE
       df
    }
    #create a bigger dataset
    df<-df[rep(1:nrow(df),each=10000),]
    system.time(res<-docendo(df))
    #   user  system elapsed 
    #  2.088   0.020   2.145
    system.time(res2<-nicola(df))
    #   user  system elapsed 
    #  0.016   0.000   0.017
    identical(res,res2)
    #[1] TRUE
    system.time(res3<-lmo(df))
    #   user  system elapsed 
    #  0.222   0.000   0.265
    identical(res2,res3)
    #[1] TRUE
    

    【讨论】:

    • 谢谢!尽管所有选项都运行良好,但由于它的速度,我标记了这个答案。
    【解决方案2】:

    尝试以下解决方案,我们首先构建一个逻辑矩阵,用于对数据进行子集化和分配 NA:

    idx <- t(apply(df != 0, 1, function(x) cumsum(x) == 0 | rev(cumsum(rev(x)) == 0)))
    df[idx] <- NA
    

    结果等于你想要的输出:

    all.equal(df, df.result)
    #[1] TRUE
    

    如果您担心性能/内存,您也可以通过两步方法执行此操作,首先计算第一列和最后一列中为 0 的行,然后仅对这些行执行第二步。

    idx1 <- rowSums(df[,c(1, ncol(df))] == 0)>0
    idx2 <- t(apply(df[idx1,] != 0, 1, function(x) cumsum(x) == 0 | rev(cumsum(rev(x)) == 0)))
    df[idx1,][idx2] <- NA
    

    附带说明,如果您使用以下方法,也可以跳过创建索引的中间步骤(尽管我更喜欢创建索引):

    is.na(df) <- t(apply(df != 0, 1, function(x) cumsum(x) == 0 | rev(cumsum(rev(x)) == 0)))
    

    【讨论】:

      【解决方案3】:

      这是另一种基本 R 方法,它使用max.col 来识别要填充的每一行的元素,然后使用矩阵子集和is.na&lt;- 来填充 NA。使用rep 和sequence 填充矩阵。

      # get the last of the 0 values from first column
      reps.first <- max.col(df, ties.method = "first") - 1
      # get the last of the 0 values starting with last column
      reps.last <- max.col(df, ties.method = "last")
      fill.last <- length(df)-reps.last
      
      # fill in from first column
      is.na(df[cbind(rep(seq_len(nrow(df))[reps.first > 0], reps.first[reps.first > 0]),
                     sequence(reps.first))]) <- TRUE
      # fill in from last column
      is.na(df[cbind(rep(seq_len(nrow(df))[fill.last > 0], fill.last[fill.last > 0]),
                     length(df)-(sequence(fill.last) - 1))]) <- TRUE
      
      all.equal(df, df.result)
      [1] TRUE
      

      【讨论】:

      • 我对我的答案进行了编辑,以将您的答案包含在基准测试中。
      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2020-11-07
      • 2018-01-23
      • 2022-11-02
      相关资源
      最近更新 更多