Using the below data frame as a rough example of what I’m trying to do, in column b I have the correct values when times = 0 but I want to code it so that the values currently blank will use the value from the column above minus the value of a in that row.
df <- data.frame(ages = c(20,20,20,20,
21,21,21,21,
22,22,22,22,
23,23,23,23),
year = c(2023,2024,2025,2026,
2023,2024,2025,2026,
2023,2024,2025,2026,
2023,2024,2025,2026),
times = c(0,1,2,3,
0,1,2,3,
0,1,2,3,
0,1,2,3),
a = c(-0.1,-0.26,-0.39,-0.48,
-0.05,-0.11,-0.23,-0.68,
-0.02,-0.13,-0.28,-0.33,
-0.01,-0.44,-0.5,-0.52),
b = c(1,””,””,””,
2,””,””,””,
3,””,””,””,
4,””,””,””))
For reference I have only recently started using R hence why I might be missing something obvious?
I have tried some mutate and lag functions but keep getting errors as I don’t think I’ve quite got the hang of what they’re doing
Adam Williams is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.