I have a small problem. I am working on a data frame. It is as follows:
df1
Duration    Intensity
NA             NA
10           0.1016
10           0.0254
NA             NA
NA             NA
10           0.0508
10           0.0508
10           0.1016
NA             NA
10           0.0254
I want to calculate the cumulative sum of “Intensity” for each events of 10 min “Duration”. Or in other words, I want to sum up the values of “Intensity” in between each ‘NA’ values in “Intensity” column. The output should look like this:
df2
Duration    Intensity   Intensity_sum
NA             NA   
10           0.1016 
10           0.0254        0.127
NA             NA   
NA             NA   
10           0.0508 
10           0.0508 
10           0.1016        0.2032
NA             NA   
10           0.0254        0.0254
I tried the following code:
library(dplyr)    
df2 <- as.data.frame(mutate(df1,Intensity_sum = with(df1,Duration==10,cumsum(Intensity))))
But I am only receiving TRUE or FALSE results, not the values.
 
     
    