I have a matrix Q_hyda with 2 columns and n rows:
[1] [2]
[1] 1950 0.265
[2] 1950 0.176
[3] 1950 0.873
. ... ...
[60] 1951 0.534
[61] 1951 0.142
. . .
. . .
. . .
. . .
[n] 2014 0.152
What I want to get is a matrix mat_HQa of this type:
[1950] [1951] [1952] ... [2014]
[1] 0.265 0.534 ... 0.152
[2] 0.176 0.142 ... ...
[3] 0.873 ... ... ...
. ... ... ... ...
. ... ... ... ...
. ... ... ... ...
[n] ... ... ... ...
I tried it with some loops:
## Create a matrix mat_HQa with a_n columns (where a_n is the number of different years) and 366 rows
mat_HQa = matrix(0, 366, a_n)
colnames(mat_HQa)=as.vector(R_a) # the vector R_a is a timeline from 1950 to 2014
# fill matrix
for (i in 1:a_n)
{for (j in 1:n)
{if (R_a[i] == Q_hyda[j,1]){mat_HQa[j,i] = Q_hyda[j,2]}}}
It works for the first column but when it moves to the second column it continues to fill the matrix mat_HQa at the j's position, and I can't figure out how to start at each column at the top.
I'm very new to programming, since it's not my subject. How can I achieve this? I sure think there's a much easier way to do this. I'm deeply grateful for any advice.