PhD candidate in Horticulture here, working on a study involving grapevine root dynamics.
I am trying to calculate a running cumulative number of roots (live roots - dead + new births) at each observation session, over three years. I have 43 observation sessions in total. Sessions 2 to 43 are birth sessions (new roots are born), and sessions 3 to 44 are sessions in when some roots died or disappeared but we also still have alive roots.
I was somewhat successful at computing cumulative sum but has not been able to develop a code that would give me the running cumulative standing crop.
This is what I have developed so far:
# make new dataframe
standingcrop <- all_roots %>%
select(BirthSession, roots, ComboDeadGone, Tube, Censored) %>%
filter(!is.na(roots)) %>%
arrange(BirthSession)
# calculate cumulative sum of roots by birthsession
standingcrop <- standingcrop %>%
group_by(BirthSession, Tube) %>%
mutate("standingcrop" = cumsum(roots))
#calculate cumulative difference - difference by deathsession
standingcrop2 <- all_roots %>%
select(BirthSession, roots, ComboDeadGone, Tube, Censored) %>%
filter(!is.na(roots)) %>%
arrange(ComboDeadGone)
standingcrop2 <- standingcrop2 %>%
group_by(BirthSession > 2, Tube) %>%
mutate("standingcrop" = cumulDiff(roots))
Does anybody have insight as how to get the cumulative standing crop (Number of roots born at a given observation session - number of roots that died or disappeared at that session?), over each observation session?
Thank you so much!