Reputation: 365
I would like to fill in the values of the lines with the value 1 from the value 1.
For examplle, gostaria de transformar:
To get:
I appreciate any help.
Upvotes: 2
Views: 208
Reputation: 887881
We could use cummax
on the rows by specifying the MARGIN = 1
in apply
. When we do the row wise, the output generated is transposed as described in ?apply
If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1. If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise. If n is 0, the result has length 0 but not necessarily the ‘correct’ dimension.
here
so do a t
on the output to get the original structure
df1[] <- t(apply(df1, 1, cummax))
Or another option is rowCummaxs
library(matrixStats)
rowCummaxs(as.matrix(df1))
Upvotes: 1