Aggregate by NA in R

后端 未结 5 1875
闹比i
闹比i 2021-01-13 11:22

Does anybody know how to aggregate by NA in R.

If you take the example below

a <- matrix(1,5,2)
a[1:2,2] <- NA
a[3:5,2] <- 2
aggregate(a[,1]         


        
5条回答
  •  迷失自我
    2021-01-13 11:58

    Instead of aggregate(), you may want to consider rowsum(). It is actually designed for this exact operation on matrices and is known to be much faster than aggregate(). We can add NA to the factor levels of a[, 2] with addNA(). This will assure that NA shows up as a grouping variable.

    rowsum(a[, 1], addNA(a[, 2]))
    #      [,1]
    # 2       3
    #     2
    

    If you still want to use aggregate(), you can incorporate addNA() as well.

    aggregate(a[, 1], list(Group = addNA(a[, 2])), sum)
    #   Group x
    # 1     2 3
    # 2   2
    

    And one more option with data.table -

    library(data.table)
    as.data.table(a)[, .(x = sum(V1)), by = .(Group = V2)]
    #    Group x
    # 1:    NA 2
    # 2:     2 3
    

提交回复
热议问题