【问题标题】:extract rows for which first non-zero element is one提取第一个非零元素为一的行
【发布时间】:2014-07-24 13:49:08
【问题描述】:

我想从数据帧my.data 中提取第一个非零元素是1 的每一行。

my.data <- read.table(text = '

     x1 x2 x3 x4
      0  0  1  1
      0  0  0  1
      0  2  1  1
      2  1  2  1
      1  1  1  2
      0  0  0  0
      0  1  0  0
', header = TRUE)

my.data

desired.result <- read.table(text = '

     x1 x2 x3 x4
      0  0  1  1
      0  0  0  1
      1  1  1  2
      0  1  0  0
', header = TRUE)

desired.result

我什至不知道从哪里开始。抱歉,如果这是重复的。感谢您的任何建议或意见。

【问题讨论】:

    标签: r


    【解决方案1】:

    这是一种方法:

    # index of rows
    idx <- apply(my.data, 1, function(x) any(x) && x[as.logical(x)][1] == 1)
    
    # extract rows
    desired.result <- my.data[idx, ]
    

    结果:

      x1 x2 x3 x4
    1  0  0  1  1
    2  0  0  0  1
    5  1  1  1  2
    7  0  1  0  0
    

    【讨论】:

    • 赢家,根据微基准测试最快的解决方案
    【解决方案2】:

    可能不是最佳答案,但是:

    rows.to.extract <- apply(my.data, 1, function(x) {
      no.zeroes <- x[x!=0]  # removing 0
      to.return <- no.zeroes[1] == 1     # finding if first number is 0
    
      # if a row is all 0, then to.return will be NA
      # this fixes that problem
      to.return[is.na(to.return)] <- FALSE # if row is all 0
    
      to.return
    })
    my.data[rows.to.extract, ]
    
      x1 x2 x3 x4
    1  0  0  1  1
    2  0  0  0  1
    5  1  1  1  2
    7  0  1  0  0
    

    【讨论】:

      【解决方案3】:
      1. 使用apply 遍历所有行:

        first.element.is.one <- apply(my.data, 1, function(x) x[x != 0][1] == 1)
        

        传递给apply 的函数将x 的第一个[1] 非零[x != 0] 元素与== 1 进行比较。每行都会调用一次,x 在您的示例中将是四个向量。

      2. 使用which 提取候选行的索引(并删除NA 值):

        desired.rows <- which(first.element.is.one)
        
      3. 选择矩阵的行——你可能知道怎么做。

      额外问题:步骤 2 中提到的 NA 值来自哪里?

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 2021-06-16
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多