1

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!

  • 1
    Welcome to stackoverflow! To make it easier for people to provide solutions, please add some example data to show the format, and also give the expected answer for the example data. – D A Wells Jul 15 '21 at 15:20
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 15 '21 at 15:58
  • It's more useful to provide a small data set and show your desired data result. – Peace Wang Jul 16 '21 at 03:19

0 Answers0