# 因子变量的累积和

``````id t1 t2 t3 t4
1   0  0  0  1
2   1  0  0  0
3   0  0  0  1
4   0  1  0  0
5   1  0  0  0
``````

``````id tc1 tc2 tc3 tc4
1   0  0  0  1
2   1  1  1  1
3   0  0  0  1
4   0  1  1  1
5   1  1  1  1
``````

``````data1[-1] <- matrixStats::rowCummaxs(as.matrix(data1[-1]))
data1
#   id t1 t2 t3 t4
# 1  1  0  0  0  1
# 2  2  1  1  1  1
# 3  3  0  0  0  1
# 4  4  0  1  1  1
# 5  5  1  1  1  1
``````

`apply`逐行的方法（也将转换为`matrix`

``````data1[-1] <- t(apply(data1[-1], 1, cummax))
``````

``````library(data.table)
dcast(melt(setDT(data1),
id = "id"
)[, value := cummax(value),
by = id],
id ~ variable)

#    id t1 t2 t3 t4
# 1:  1  0  0  0  1
# 2:  2  1  1  1  1
# 3:  3  0  0  0  1
# 4:  4  0  1  1  1
# 5:  5  1  1  1  1
``````

``````library(dplyr)
library(tidyr)
data1 %>%
gather(variable, value, -id) %>%
group_by(id) %>%
mutate(value = cummax(value)) %>%

# Source: local data frame [5 x 5]
# Groups: id [5]
#
#      id    t1    t2    t3    t4
#   (int) (int) (int) (int) (int)
# 1     1     0     0     0     1
# 2     2     1     1     1     1
# 3     3     0     0     0     1
# 4     4     0     1     1     1
# 5     5     1     1     1     1
``````

``````data1[-1] <- Reduce(pmax, data1[-1], accumulate = TRUE)
data1
#   id t1 t2 t3 t4
# 1  1  0  0  0  1
# 2  2  1  1  1  1
# 3  3  0  0  0  1
# 4  4  0  1  1  1
# 5  5  1  1  1  1
``````

0条评论