【问题标题】:Sum by Rows in RR中按行求和
【发布时间】:2015-04-14 23:54:14
【问题描述】:

让我重新提出我的问题,我如何按行对数字求和,并在最后一列后面列出总和,形成一个像第二个表一样的新列 (sum = a + b+ c + d + e)?

而且我还想知道,如果某些值是 N/A,我还能将它们视为数字吗?

示例输入:

     a          b           c          d            e
1    90         67          18         39           74
2    100        103         20         45           50 
3    80         87          23         44           89
4    95         57          48         79           90
5    74         81          61         95           131

期望的输出:

     a          b           c          d            e    sum
1    90         67          18         39           74   288
2    100        103         20         45           50   318
3    80         87          23         44           89   323
4    95         57          48         79           90   369
5    74         81          61         95           131  442 

【问题讨论】:

  • 您可以直接发布您的代码,无需发布图片
  • 只是做了,但不是代码。
  • @MrFlick 使用带有 addmargins 的 rowsum 的答案是标准答案,如果这不起作用发布您尝试过的 代码 并解释您遇到的问题它

标签: r sum


【解决方案1】:

要添加行总和,可以使用addmargins

M <- matrix(c(90,67,18,39,74), nrow=1)
addmargins(M, 2)   #2 = row margin
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   90   67   18   39   74  288

如果您缺少数据,则需要将边距函数更改为能够正确处理 NA 值的函数

M<-matrix(c(90,67,18,NA,74), nrow=1)
addmargins(M, 2, FUN=function(...) sum(..., na.rm=T))
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   90   67   18   NA   74  249

【讨论】:

  • 或者避免重新定义函数 - cbind(M,rowSums(M,na.rm=TRUE))
  • 感谢@MrFilck 的快速答复。但也许我没有把我的问题说清楚,我只是收回我的问题,你能再帮我一次吗?
  • 我的解决方案有什么问题?你还没有真正做好reproducible example。所涉及的对象的类别是什么?矩阵?一张桌子?一个data.frame? M&lt;-structure(c(90L, 100L, 80L, 95L, 74L, 67L, 103L, 87L, 57L, 81L, 18L, 20L, 23L, 48L, 61L, 39L, 45L, 44L, 79L, 95L, 74L, 50L, 89L, 90L, 131L), .Dim = c(5L, 5L), .Dimnames = list(c("1", "2", "3", "4", "5"), c("a", "b", "c", "d", "e"))); addmargins(M,2) 似乎工作得很好。如果您有 data.frame,请使用 addmargins(as.matrix(M),2)
  • 那是一个矩阵。很抱歉造成误会,我是R的菜鸟。
【解决方案2】:

考虑使用 apply()。例如:

set.seed(10) # optional, but this command will replicate data as shown

# create some data
x <-matrix(rnorm(1:25),nrow=5,ncol=5) # 5x5 matrix of random numbers
x
            [,1]       [,2]       [,3]        [,4]       [,5]
[1,]  0.01874617  0.3897943  1.1017795  0.08934727 -0.5963106
[2,] -0.18425254 -1.2080762  0.7557815 -0.95494386 -2.1852868
[3,] -1.37133055 -0.3636760 -0.2382336 -0.19515038 -0.6748659
[4,] -0.59916772 -1.6266727  0.9874447  0.92552126 -2.1190612
[5,]  0.29454513 -0.2564784  0.7413901  0.48297852 -1.2651980

x.sum <-apply(x,1,sum) # sum the rows. Note: apply(x,2,sum) sums cols

x.sum
[1]  1.003356605 -3.776777904 -2.843256446 -2.431935624 -0.002762636

# attach new column (x.sum) to matrix x  
x.sum.1 <-cbind(x,x.sum)

x.sum.1
                                                                    x.sum
[1,]  0.01874617  0.3897943  1.1017795  0.08934727 -0.5963106  1.003356605
[2,] -0.18425254 -1.2080762  0.7557815 -0.95494386 -2.1852868 -3.776777904
[3,] -1.37133055 -0.3636760 -0.2382336 -0.19515038 -0.6748659 -2.843256446
[4,] -0.59916772 -1.6266727  0.9874447  0.92552126 -2.1190612 -2.431935624
[5,]  0.29454513 -0.2564784  0.7413901  0.48297852 -1.2651980 -0.002762636

【讨论】:

    【解决方案3】:

    假设你有数据框df,那么你可以尝试这样的事情:

    # Assuming the columns a,b,c,d,e are at indices 1:5
    df$sum = rowSums(df[ , c(1:5)], na.rm = T)
    

    或者你也可以试试这个:

    transform(df, sum=rowSums(df), na.rm = T)
    

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 2020-11-26
      • 1970-01-01
      • 2020-06-20
      • 2021-09-16
      • 1970-01-01
      • 2023-03-20
      • 2016-07-25
      • 2020-08-26
      相关资源
      最近更新 更多