假设我有一个
形式的矩阵(或向量)var outputPDFDocument = PdfDocument.Combine("Output.pdf", "doc1.pdf", "doc2.pdf", "doc3.pdf");
outputPDFDocument.Save("C:\\temp.pdf");
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";
PdfFilePrinter.DefaultPrinterName = "Take printer";
PdfFilePrinter printer = new PdfFilePrinter(@"C:\\temp.pdf");
printer.Print();
如何计算每列中1之间的连续零的数量,并将那些数量小于预定义常数k的零替换为0。或者至少获得每个零序列中的起始索引和元素数量。 通常,此数据集中的零比实际值多得多,并且大多数情况下序列的长度大于k
因此,例如,如果k = 1,那么[4,2]; [13,1]和[15,1]将被替换为1.如果k = 2,则除了[4, 1]; [13,1]和[15,1],[3,1],[4,1],[14,2]和[15,2]中的零也将被1替换在这个例子中。
当然,我可以运行一个循环并遍历所有行。我想知道是否有一个包,或一个可以做到的整洁的矢量化技巧。
更新
k = 1
的期望输出示例>set.seed(1)
>X=ifelse(matrix((runif(30)),ncol = 2)>0.4,0,1)
[,1] [,2]
[1,] 1 1
[2,] 1 1
[3,] 0 1
[4,] 0 0
[5,] 1 1
[6,] 0 0
[7,] 0 0
[8,] 0 0
[9,] 0 1
[10,] 1 0
[11,] 1 0
[12,] 1 0
[13,] 0 1
[14,] 1 0
[15,] 0 0
...
etc
k = 2的期望输出
[,1] [,2]
[1,] 1 1
[2,] 1 1
[3,] 0 1
[4,] 0 1
[5,] 1 1
[6,] 0 0
[7,] 0 0
[8,] 0 0
[9,] 0 1
[10,] 1 0
[11,] 1 0
[12,] 1 0
[13,] 1 1
[14,] 1 0
[15,] 1 0
答案 0 :(得分:4)
游程工具rle
在这里工作:
fill_shortruns <- function(X,k=1,badval=0,newval=1){
apply(X,2,function(x){
r <- rle(x)
r$values[ r$lengths <= k & r$values == badval ] <- newval
inverse.rle(r)
})
}
# smaller example
set.seed(1)
X0=ifelse(matrix((runif(10)),ncol = 2)>0.4,0,1)
# [,1] [,2] [,3] [,4]
# [1,] 1 0 1 0
# [2,] 1 0 1 0
# [3,] 0 0 0 0
# [4,] 0 0 1 1
# [5,] 1 1 0 0
fill_shortruns(X0,2)
# [,1] [,2] [,3] [,4]
# [1,] 1 0 1 0
# [2,] 1 0 1 0
# [3,] 1 0 1 0
# [4,] 1 0 1 1
# [5,] 1 1 1 1