将行乘以条目的数值

multiply rows by the numerical value of an entry

在 R 中,我有一个具有多个分类值的矩阵。索引大小为 2 平方米、4 平方米、6 平方米,单元数为 1-3,人数为 1-4,然后是包含所有出现次数的汇总计数的列。

例如:

Size;Units;Pers;Count
4;3;4;3 # three time this row
2;1;1;2 # two times this row
6;2;2;1 # one times this row

如何使最后的 column/vector 行相乘以便打印出来:

Size;Units;Pers;Count
4;3;4;1
4;3;4;1
4;3;4;1
2;1;1;1
2;1;1;1
6;2;2;1

在电子表格或 R 中。 这是学校的作业,我只是找不到制作最后一个向量的方法(我将其用作常量来乘以前 3 列,但仍然在最后一列条目中保留一个。

我们可以通过 'Count' 列和 transform 复制行序列以创建 1 的 'Count' 列。

transform(df1[rep(1:nrow(df1), df1$Count),-4], Count=1)

这也可以通过 library(splitstackshape)

中的包装函数 expandRows 来完成
library(splitstackshape)
transform(expandRows(df1, 'Count'), Count=1)