我有一个data.frame包含几个列(即V1... Vn+1),其值为1或0,每列是一个时间步长.
我想知道time值1之间的平均值(列数).序列的1 1 1 1 1 1值为1.
目前我可以想到计算这个的方法是计算1s之间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考虑到以零开头或结尾的问题,有没有办法计算by行值之间的列数?
# 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.
Run Code Online (Sandbox Code Playgroud)
如果第一个和最后一个值之间的k范围是n,并且该范围内的总数为1 ,则平均间隙为(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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |