Rowwise Cumulative Sum

Calculating cumulative sum for each row

You want cumsum()

df <- within(df, acc_sum <- cumsum(count))

Rowwise cumulative sum

Another option use Reduce with accumulate=TRUE:

dt[, names(dt) := Reduce(`+`, dt, accumulate = TRUE)]

dt
# t1 t3 t7
#1: 0 12 37
#2: 0 5 53
#3: 0 8 15
#4: 0 9 18
#5: 0 5 19

Row-wise cumulative sum over some columns, followed by row-wise division

dat <- structure(list(New_Sign_ups = c(100L, 50L), Spend = c(100L, 200L
), `2021-05` = c(0L, 10L), `2021-06` = c(10L, 10L), `2021-07` = c(100L,
40L), MRR_sum = c(110L, 60L), time = c(1.5, 1.8)), class = "data.frame",
row.names = c(NA, -2L))

Without using any packages

dat[3:5] <- do.call("cbind", Reduce(`+`, dat[3:5], accumulate = TRUE)) / dat$Spend
# New_Sign_ups Spend 2021-05 2021-06 2021-07 MRR_sum time
#1 100 100 0.00 0.1 1.1 110 1.5
#2 50 200 0.05 0.1 0.3 60 1.8

Using package matrixStats

library(matrixStats)
dat[3:5] <- rowCumsums(as.matrix(dat[3:5])) / dat$Spend
# New_Sign_ups Spend 2021-05 2021-06 2021-07 MRR_sum time
#1 100 100 0.00 0.1 1.1 110 1.5
#2 50 200 0.05 0.1 0.3 60 1.8

how to add up the column (cumulative sum) of the matrix in R?

We can apply cumsum on each row by looping over the rows with apply and MARGIN specified as 1 and transpose the output

t(apply(m1, 1, cumsum))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8

Or with a for loop

for(i in seq_len(ncol(m1))[-1]) m1[,i] <- m1[, i] + m1[, i-1]

Or another option is to split it a list of vectors with asplit and then Reduce with + and accumulate = TRUE

do.call(cbind, Reduce(`+`, asplit(m1, 2), accumulate = TRUE))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8

or with a convenient function rowCumsums from matrixStats

library(matrixStats)
rowCumsums(m1)
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8

data

m1 <- cbind(1:2, 3:4, 1:2)

Cumulative Sum over an Dataframe

Does this work:

library(dplyr)
library(tidyr)

data %>% pivot_longer(-1) %>% group_by(Delivery.Year) %>%
mutate(value = cumsum(value)) %>%
pivot_wider(Delivery.Year, names_from = name, values_from = value)

# A tibble: 4 x 5
# Groups: Delivery.Year [4]
Delivery.Year Year.1976 Year.1977 Year.1978 Year.1979
<chr> <dbl> <dbl> <dbl> <dbl>
1 1976 10 15 25 38
2 1977 3 3 13 13
3 1978 8 13 13 13
4 1979 0 0 0 14

Row-level cumulative sum with condition

you can use cumsum on axis=1 with get_indexer on the df.columns:

df['output'] = df.columns.get_indexer(df.drop("s",1).cumsum(axis=1)
.ge(df['s'],axis=0).idxmax(axis=1))+1


print(df)

m1 m2 m3 m4 m5 m6 m7 m8 s output
0 0 1 0 0 5 0 4 10 4 5
1 4 1 8 0 15 0 4 10 10 3

EDIT:

There can be situations where none of the column in a row satisfies this condition , in that case, you may use a condition to check (expect a -1 where the condition doesnot match for any column in a row):

c = df.drop("s",1).cumsum(axis=1).ge(df['s'],axis=0)
df['output'] = df.columns.get_indexer(c.idxmax(1).where(c.any(1)))+1

Cumulative sum on preceding rows in the same column - R

test %>% 
mutate(
cumsum_1 = cumsum(lag(loss_to_layer, default = 0)),
new_col = pmin(loss_to_layer, 127000000 - cumsum_1),
new_col = ifelse(new_col < 0, 0, new_col)
) %>%
select(-cumsum_1)

Cumulative sum but conditionally excluding earlier rows

With the help of NumPy:

# sum without conditions
raw_sum = df.groupby("val_a", sort=False).quantity.sum().cumsum()

# comparing each `val_b` against each unique `val_a` via `gt.outer`
sub_mask = np.greater.outer(df.val_b.to_numpy(), df.val_a.unique())

# selecting values to subtract from `quantity` and summing per `val_a`
to_sub = (sub_mask * df.quantity.to_numpy()[:, np.newaxis]).sum(axis=0)

# subtracting from the raw sum
result = raw_sum - to_sub

to get

>>> result.reset_index()

val_a quantity
0 3 7
1 2 26
2 1 18


Related Topics



Leave a reply



Submit