替换在R中数据帧任一端找到的连续零

标签 r

我需要用 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

提前致谢。

最佳答案

另一种方式,避免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

关于替换在R中数据帧任一端找到的连续零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43917830/

相关文章:

r - dplyr 在标记的列上加入中断(避风港)

r - 合并数据框列集

r - 如何使用索引字符串从 R 中的数据帧或矩阵中提取行

css - Shiny - 如何设置选定单选按钮标签的样式?

randomForest的重要性只包含MeanDecreaseGini

r - 从查找表中更新向量的某些值的规范 tidyverse 方法

r - 基于单个单元格内的 % 符号排序 - R

r - 如何绘制混合模型的结果

r - 如何通过具有多个单位的输出获得时差

r - 如果值存在于具有匹配 ID 的其他矩阵行中的任何位置,则使用 "1"-- R 填充单元格