使用向量化函数计算向量中连续零的最大数量
Count largest number of consecutive zeros in a vector using a vectorized function
我想构建一个函数,它将一个向量作为参数,returns 最大数量的连续零。例如:
a <- c(0,0,1,1,0)
b <- c(0,1,3,10,0,0,0)
x <- count_max_consecutive_zeros(a)
y <- count_max_consecutive_zeros(b)
应该导致 x=2 和 y=3。我可以寻求明显的解决方案并进行循环:
count_max_consecutive_zeros <- function(x) {
max_count <- 0
current_count <- 0
for (i in 1:length(x) {
if(x[i] == 0) {
current_count = current_count + 1
} else {
if(current_count > max_count) {
max_count <- current_count
}
current_count <- 0
}
}
这个解决方案适用于短向量,但是我必须在数万个条目长的向量上使用这个函数数千次,所以恐怕我会 运行性能问题。是否有等价于 count_max_consecutive_zeros
的向量化函数?
您可以使用 rle
和 max
来 计算连续零的最大数量。
x <- rle(a==0)
max(x$lengths[x$values == TRUE])
#[1] 2
选项rleid
library(data.table)
max(tapply(a[a==0], rleid(a)[a == 0], FUN = length))
#[1] 2
我想构建一个函数,它将一个向量作为参数,returns 最大数量的连续零。例如:
a <- c(0,0,1,1,0)
b <- c(0,1,3,10,0,0,0)
x <- count_max_consecutive_zeros(a)
y <- count_max_consecutive_zeros(b)
应该导致 x=2 和 y=3。我可以寻求明显的解决方案并进行循环:
count_max_consecutive_zeros <- function(x) {
max_count <- 0
current_count <- 0
for (i in 1:length(x) {
if(x[i] == 0) {
current_count = current_count + 1
} else {
if(current_count > max_count) {
max_count <- current_count
}
current_count <- 0
}
}
这个解决方案适用于短向量,但是我必须在数万个条目长的向量上使用这个函数数千次,所以恐怕我会 运行性能问题。是否有等价于 count_max_consecutive_zeros
的向量化函数?
您可以使用 rle
和 max
来 计算连续零的最大数量。
x <- rle(a==0)
max(x$lengths[x$values == TRUE])
#[1] 2
选项rleid
library(data.table)
max(tapply(a[a==0], rleid(a)[a == 0], FUN = length))
#[1] 2