I want to divide each cell by the previous cell in the same column and generate a new calculated column that has the new values.
Here is a solution using the lag()
function from dplyr
.
library(dplyr, warn.conflicts = FALSE)
data <- data.frame(col = c(4, 8, 32, 64))
mutate(data, new_col = col / lag(col))
#> col new_col
#> 1 4 NA
#> 2 8 2
#> 3 32 4
#> 4 64 2
Created on 2020-04-18 by the reprex package (v0.3.0)
1 Like
Thanks so much for your reply. it works
Great! If I solved your problem, please consider marking my post as a solution.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.