i have series of net carbon uptake values (ncu) time. want create new series of net carbon release values (ncr) time through loop based on following:
- the ncr @ time
t
sum ofc
release during period of other times older timet
. - the
c
release during period @ timet
can calculated difference of potential ncus @ timet
, timet−1
. - ncu added @ beginning of each period.
the paper using base model off of states mathematical trick keeping track of decomposition @ various time intervals. equation ncrsubt = sum(ncusub[t] - ncusub[t-1])
t=1
t = t
.
this have in r far:
dt = 0.005 # time step in years duration = 15 # simulation runs 15, 500 yr intervals n = duration/dt + 1 # number of step including starting point time = (0:(n-1))*dt # time in unit 500 yr intervals ncr = numeric(n) (i in 1:(n-1)){ ncr[i] = ncu[i] + (ncu[i] - ncu[i-1]) }
i can't figure out how accomplish making ncr[i]
sum of loop above times less given time.
ncu has 2 variables, time(x)
(in 500 year intervals) 1:15
, , ncu (y)
.
edit
here sample values:
ncu values
ncu = c(52.02080416,55.0440176,77.09245546,80.12810245,71.14214209,60.14417294,36.10094125,66.21153828,64.23081522,61.24448865,54.23812349)
ncut - ncut-1 values
ncut_ncut_1 = c(-3.023213444,-22.04843786,-3.035646992,8.98596036,10.99796916,24.04323169,-30.11059703,1.980723063,2.986326567,7.006365164)
ncrt values
ncrt = c(10.77406468,13.79727813,35.84571598,38.88136298,29.89540262,18.89743346,-5.145798226,24.9647988,22.98407574,19.99774917)
excel screenshot of calculations:
okay, let's quick answer this.
# load in data # includes data within screenshot (1:15) vs. (1:11) ncu = c(52.02080416,55.0440176,77.09245546,80.12810245,71.14214209,60.14417294,36.10094125,66.21153828,64.23081522,61.24448865,54.23812349, 65.31275,58.30238552, 51.28640117, 41.24673948) # reverse difference e.g. -1*([t] - [t-1]) => [t-1]-[t] dncu = -1*diff(ncu) # here function use within `sapply()` # function provides sum x length of differenced # ncu data. calc_ncr = function(x, dncu, len_dncu){ sum(dncu[x:len_dncu]) } # length of differenced ncu data len_dncu = length(dncu) # vectorized loop speed. # range given 1 length of differenced data. sapply(1:len_dncu, fun = calc_ncr, dncu = ncut_ncut_1, len_dncu = len_dncu)
result (after using as.matrix()
on sapply()
make pretty):
[,1] [1,] 10.774065 [2,] 13.797278 [3,] 35.845716 [4,] 38.881363 [5,] 29.895403 [6,] 18.897433 [7,] -5.145798 [8,] 24.964799 [9,] 22.984076 [10,] 19.997749 [11,] 12.991384 [12,] 24.066011 [13,] 17.055646 [14,] 10.039662