【问题标题】:How to count the number of observations for a specific condition in R?如何计算R中特定条件的观察次数?
【发布时间】:2021-11-03 01:56:59
【问题描述】:

我有一个这样的数据集:

data <- data.frame(ID = c(1,1,1,1,1,2,2,2,2),
                   year = c(1,2,3,4,5,1,2,3,4),
                   score = c(0.89943475,-3.51761975,1.54511640,-1.38284380,2.45591240,-1.89925250,0.83935451,-0.61843636,-0.70421765)

ID, year, score
1, 1, 0.89943475
1, 2, -3.51761975
1, 3, 1.54511640
1, 4, -1.38284380
1, 5, 2.45591240
2, 1, -1.89925250
2, 2, 0.83935451
2, 3, -0.61843636
2, 4, -0.70421765

我想创建一个数据表,汇总上述数据并计算IDscore 为正和负时的观察次数,如下所示:

ID, pos, neg, total
 1,   3,   2,     5
 2,   1,   3,     4

这可以在 R 中使用 data.table 吗?

【问题讨论】:

    标签: r group-by count data.table aggregate


    【解决方案1】:

    我们可以使用dcastsign

    library(data.table)
    dcast(setDT(data), ID ~ sign(score), fun.aggregate = length)[,
          total := rowSums(.SD), .SDcols = -1][]
    

    -输出

       ID -1 1 total
    1:  1  2 3     5
    2:  2  3 1     4
    

    【讨论】:

      【解决方案2】:

      akrun 答案的替代方案:

      data[, .(pos = sum(score >= 0), neg = sum(score < 0), total = .N), by = ID]
      #       ID   pos   neg total
      #    <num> <int> <int> <int>
      # 1:     1     3     2     5
      # 2:     2     1     3     4
      

      数据

      data <- setDT(structure(list(ID = c(1, 1, 1, 1, 1, 2, 2, 2, 2), year = c(1, 2, 3, 4, 5, 1, 2, 3, 4), score = c(0.89943475, -3.51761975, 1.5451164, -1.3828438, 2.4559124, -1.8992525, 0.83935451, -0.61843636, -0.70421765)), class = c("data.table", "data.frame"), row.names = c(NA, -9L)))
      

      【讨论】:

        猜你喜欢
        • 2019-12-07
        • 1970-01-01
        • 2012-03-14
        • 2022-12-06
        • 1970-01-01
        • 2014-07-06
        • 2022-12-08
        • 2021-05-06
        • 1970-01-01
        相关资源
        最近更新 更多