【发布时间】:2015-10-25 22:33:41
【问题描述】:
我有一个 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.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.
【问题讨论】: