我有一个 data.frame,其中包含多个值为 1 或 0 的列(即 V1
... Vn+1
),每列都是一个时间步长。
我想知道平均值time
(列数)介于 1 之间。序列为 1 1 1 1 1 1
值为 1
。
目前我认为可能计算这个的方法是计算 1 之间 0 的平均计数 (+1),但它是有缺陷的。
例如,具有这些值 1 0 0 1 0 1
的行将会得到结果 2.5
( 2 + 1
= 3
; 3/2
= 1.5
; 1.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
关于r - 按行获取 x 值之间的平均列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31793728/