I have a data.frame with several measurements of trees diameter. What I'm trying to do is compute de cumulative sum of the variable dbh_increase, which is product of a mutate operation (hope I'm been clear here).
My data.frame: https://www.dropbox.com/s/9usbu2kubbdyheu/bddendro.csv?dl=0
And here's the whole code I'm running:
bddendro<-read.table("bddendro.csv", h=T, sep = ";", dec = ",")
bddendro$dbh_new<-(bddendro$cbh_init + (bddendro$dendro_length * 0.2))/pi
bddendro<- bddendro %>%
filter(med != 0) %>%
group_by(parc, tree) %>%
mutate(dbh_increase = ifelse(dendro_length < lag(dendro_length), 0 ,dbh_new - lag(dbh_new))) %>%
mutate(dbh_cumsum = cumsum(dbh_increase))
The first mutate() works fine, at least as I'd expect, the second one that isn't working. Returning just NA values
SOLUTION:
cumsum() doesn't handle NA values, so I used mutate() to change NAs to 0, the code below:
mutate(dbh_increase = ifelse(is.na(dbh_increase), 0, dbh_increase))