I have the following function to build a stock effect for a variable in one column. The variable creates a value in Column B that takes the value in ColumnA and adds a carry over (like e.g. 0.5) from the previous observation in Column B.
constructZ <- function(lag, N) {
r <- lag^(seq_len(N)-1)
m <- matrix(rep(r,N),nrow=N)
z <- matrix(0,nrow=N,ncol=N)
z[lower.tri(z,diag=TRUE)] <- m[row(m) <= (N+1-col(m))]
z
}
My problem is now that I have a panel data set that has in one column observations for many different cases. Each case has a specific indicator (numeric). Data looks like:
ColumnA Indicator Time
1 1 1
0 1 2
0 1 3
4 2 1
5 2 2
0 2 3
4 3 1
0 3 2
2 3 3
I now want the function to be applied to each case (Indicator) for all observations (Time).
Any idea how to achieve this? The Output should then look like:
ColumnA Indicator Time ColumnB
1 1 1 1
0 1 2 0.5
0 1 3 0.25
4 2 1 4
5 2 2 7
0 2 3 3.5
4 3 1 4
0 3 2 2
2 3 3 3
Any help or support is highly appreciated!
Many thanks in advance!