I'm reading in some XY data from a file, and need to do a cumulative sum on the Y ordinate with steps coarser than that in the original data.
How can I do a cumsum on a the Y column based on X values in other columns?
library(tidyverse)
x_data <- seq(2, 5, 0.2) # this is read in from a file
y_data <- runif(length(x_data)) # this is read in from a file
input_data <- tibble(x_data, y_data)
x_steps <- seq(2, 5, 1)
final_data = tibble(x_steps)
#how to cumsum input_data$y_data based on final_data$x_steps?
#x_data and x_steps are guarenteed to be strictly increasing
*edit: Some example data:
# input_data
2.0 0.55005588
2.2 0.27577714
2.4 0.24215415
2.6 0.70794255
2.8 0.30632029
3.0 0.71693474
3.2 0.49712663
3.4 0.21227011
3.6 0.74823731
3.8 0.90176957
4.0 0.52331898
4.2 0.79409880
4.4 0.04228391
4.6 0.06220941
4.8 0.50969235
5.0 0.11734668
should turn into:
#final_data
2.0 0.55005588
3.0 2.79918475
4.0 5.68190735
5.0 7.2075385