【问题标题】:By row, get mean count of number of columns between values of x按行,获取 x 值之间的平均列数
【发布时间】: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.

【问题讨论】:

    标签: r dplyr apply


    【解决方案1】:

    如果第一个和最后一个 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
    

    【讨论】:

    • 不错的方法;对此的另一种看法可能是 mdf1 = as.matrix(df1); (max.col(mdf1, "last") - max.col(mdf1, "first")) / (rowSums(mdf1) - 1) 并适当地插入 NAs。
    猜你喜欢
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    相关资源
    最近更新 更多