如何在R中按行累加总和

时间:2018-11-19 11:33:57

标签: r dataframe sum

我想对同一行中的每一列进行一次行总和(累加)。我看到的是这样的东西:

test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
> test
  Position Position1 Position2 Position3 Position4
1        0         0         0         0         0
2        1         0         0         0         0
3        1         1         0         0         0
4        1         1         1         0         0
5        0         1         1         1         0
6        0         0         1         1         1

result = data.frame(c(0,1,2,3,0,0),c(0,0,1,2,3,0),c(0,0,0,1,2,3),c(0,0,0,0,1,2),c(0,0,0,0,0,1))
colnames(result) = c('Position','Position1','Position2','Position3','Position4')
> result
  Position Position1 Position2 Position3 Position4
1        0         0         0         0         0
2        1         0         0         0         0
3        2         1         0         0         0
4        3         2         1         0         0
5        0         3         2         1         0
6        0         0         3         2         1

data.frame结果就是我想要的。

谢谢

2 个答案:

答案 0 :(得分:2)

起初我以为您只需要对每一列应用cumsum(),但是看来您还需要将累加总和乘以列本身以确保保留零:

test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
apply(test, 2, function(x) x * cumsum(x))
#>      Position Position1 Position2 Position3 Position4
#> [1,]        0         0         0         0         0
#> [2,]        1         0         0         0         0
#> [3,]        2         1         0         0         0
#> [4,]        3         2         1         0         0
#> [5,]        0         3         2         1         0
#> [6,]        0         0         3         2         1

reprex package(v0.2.1)于2018-11-19创建

如果您确实只想为每一列求和,则可以使用apply(df, 2, cumsum)

test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
apply(test, 2, cumsum)
#>      Position Position1 Position2 Position3 Position4
#> [1,]        0         0         0         0         0
#> [2,]        1         0         0         0         0
#> [3,]        2         1         0         0         0
#> [4,]        3         2         1         0         0
#> [5,]        3         3         2         1         0
#> [6,]        3         3         3         2         1

reprex package(v0.2.1)于2018-11-19创建

答案 1 :(得分:1)

类似于@duckmayr

<xsl:template match="HeaderInformation[PackingslipId != LineItem/Lines/PackingslipId]/*[self::PackingslipId or self::DeliveryDate]" />

更多细节:

apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

#     Position Position1 Position2 Position3 Position4
#[1,]        0         0         0         0         0
#[2,]        1         0         0         0         0
#[3,]        2         1         0         0         0
#[4,]        3         2         1         0         0
#[5,]        0         3         2         1         0
#[6,]        0         0         3         2         1