按行,获取 x 值之间的平均列数

By row, get mean count of number of columns between values of x

我有一个 data.frame,其中包含多个值为 1 或 0 的列(即 V1...Vn+1),每列都是一个时间步长。

我想知道值 1 之间的平均值 time(列数)。1 1 1 1 1 1 的序列的值为 1

目前我能想到的计算方法是计算 1 之间 0 的平均计数 (+1),但它是有缺陷的。

例如,具有这些值 1 0 0 1 0 1 的行将具有结果 2.52 + 1 = 33/2 = 1.51.5 + 1 = 2.5)。

但是,如果序列以 0 开头或结尾,则计算此结果的结果时应不带它们。例如,0 1 0 0 1 1 将计算为 1 0 0 1 1,结果为 3

有缺陷 例如1 0 1 1 0 0 将计算为 1 0 1 1,结果为 2 这不是所需的结果 (1.5)

考虑到以零开头或结尾的问题,有没有办法按行计算 1 值之间的列数?

# example data.frame with desired result
df <- structure(list(Trial = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Location = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L), Position = c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), V1 = c(1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), V2 = c(1L, 
1L, 1L, 0L, 1L, 0L, 0L, 0L), V3 = c(1L, 1L, 1L, 0L, 1L, 0L, 0L, 
1L), V4 = c(1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), V5 = c(1L, 0L, 0L, 
0L, 1L, 0L, 0L, 0L), V6 = c(1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L), 
    Result = c(1, 3, 2, NA, 1, 2.5, 3, 1.5)), .Names = c("Trial", 
"Location", "Position", "V1", "V2", "V3", "V4", "V5", "V6", "Result"
), class = "data.frame", row.names = c(NA, -8L))

df1 <- df[,4:9]

#This code `apply(df1,1,function(x) which(rev(x)==1)[1])) calculates the number of columns back until a value of 1, or forward without `rev`. But this doesn't quite help with the flaw.

如果第一个和最后一个 1 值之间的范围是 k,并且该范围内的 1 的总数是 n,则平均差距是 (k-1)/(n-1)。您可以使用以下方法计算:

apply(df1, 1, function(x) {
  w <- which(x == 1)
  if (length(w) <= 1) NA
  else diff(range(w)) / (length(w)-1)
})
# [1] 1.0 2.0 2.0  NA 1.0 2.5 3.0 1.5