简体   繁体   English

汇总(和相乘)行中的行-R

[英]Summing (and multiplying) the rows in a coulmn - R

I am trying to sum (and multiply) all my rows in a table to the final output would be just one row and multiple columns. 我试图将一个表中的所有行相加(并相乘)到最终输出将只是一行和多列。

My data looks like this: 我的数据如下所示:

##      Typ-1 1m Typ-1 2m Typ-2 1m Typ-2 2m Typ-3 1m Typ-3 2m Typ-4 1m
##   1        15      119        3       28        3       20        1
##   2         1       88        0       23        0       18        1
##   3         0       65        0       17        0       11        0
##   4         3       49        0       14        0        4        0
##   5         0       41        0        6        0        1        0
##   6         0       16        0        0        0        3        0
##   7         0        4        0        4        0        2        0

each row represents how manny of the items were bought. 每行表示如何购买许多物品。 For example the 4th row in the first column "Typ-1 1m" says that we sold 12 items --> (4*3) 例如,第一列“ Typ-1 1m”的第四行说我们卖出了12件商品->(4 * 3)

I would like my final output to be across all columns as te summary of each rows, however the number of a row must first multiply with the value in a row. 我希望最终输出跨所有列,作为每行的摘要,但是行数必须首先与行中的值相乘。

the output in a first coulmn would look like this: (15*1)+(1*2)+(3*4)=29 第一个库仑的输出看起来像这样:(15 * 1)+(1 * 2)+(3 * 4)= 29

I have no idea how to solve this in R. 我不知道如何在R中解决这个问题。

I have only managed to print out a table.. I would appreciate the help! 我只设法打印了一张桌子。

We multiply the 'dat' with the row of that and get the column sums ( colSums ) 我们将'dat'乘以该row ,然后获得列总和( colSums

colSums(dat*row(dat))
#   Typ.1_1m Typ.1_2m Typ.2_1m Typ.2_2m Typ.3_1m Typ.3_2m Typ.4_1m 
#      29     1015        3      239        3      142        3 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM