0

I am in my first steps of coding in R.

I am trying to fill a matrix, which is based on some vectors inside a matrix. This is my matrix:

 OPEN  NET_CONTRIBUTION RETURN CONTRIBUTION_RETURN SAVING_RETURN CLOSE
[1,] 0     900              0.019  17.1                0             ?    
[2,] 917.1 900              -0.006 -5.4                0             ?    
[3,] ?     900              0.004  3.6                 0             ?    
[4,] 903.6 900              0.004  3.6                 0             ?    
[5,] 903.6 900              0.002  1.8                 0             ?    
[6,] 901.8 900              0.002  1.8                 0             ?    

NET_CONTRIBUTION is a vector with a constant number. Return is a vector of random values.

  • CONTRIBUTION_RETURN is the calculation of NET_CONTRIBUTION * (1+RETURN).
  • SAVING_RETURN should be Open*(1+RETURN)
  • CLOSE should be OPEN + NET_CONTRIBUTION + CONTRIBUTION_RETURN + SAVING RETURN

  • OPEN is the CLOSE value for the upper row.

So I tried this code, and it doesn't work.

for (i in 1:444){
  Investment[i,5] <-  Investment[i,1]*(1+Investment[i,3]) #Saving Return
  Investment[i,6] <-  Investment[i,1] + Investment[i,2] + Investment[i,4] Investment[i,5] # CLOSE
  Investment[i+1,1] <-  (Investment[i,6]) # OPEN in the next row
}

I get the following message:

> > for (i in 1:444){
> +   Investment[i,5] <-  Investment[i,1]*(1+Investment[i,3])
> +   Investment[i,6] <-  Investment[i,1] + Investment[i,2] + Investment[i,4] Investment[i,5] Error: unexpected symbol in: " 
> Investment[i,5] <-  Investment[i,1]*(1+Investment[i,3])  
> Investment[i,6] <-  Investment[i,1] + Investment[i,2] +
> Investment[i,4] Investment"
> >   Investment[i+1,1] <-  (Investment[i,6])

Maybe I am not aprroaching in the right way this idea. Thank you all for your help, Tom

TomTr
  • 1
  • 2
    Hi TomTr. Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! The matrix example you provided is **not** easily usable. That being said: In your code, the line `Investment[i,6] <- Investment[i,1] + Investment[i,2] + Investment[i,4] Investment[i,5]` has no operator between `Investment[i,4]` and `Investment[i,5]`. That's what the error is telling you. – dario Feb 09 '20 at 15:28

2 Answers2

1

You just have a missing + symbol in the computation of the close (Investment[i,6])

You should have

Investment[i,6] <-  Investment[i,1] + Investment[i,2] + Investment[i,4] + Investment[i,5]

but instead you have

Investment[i,6] <-  Investment[i,1] + Investment[i,2] + Investment[i,4] Investment[i,5]

Note the missing + between Investment[i,4] and Investment[i,5]

djbetancourt
  • 347
  • 3
  • 11
0

In R we have this concept of vectorization, which applies whatever you do to the whole vector (or matrix). So you really do not need loops. Most stuff is way easier than you think. For example:

NET_CONTRIBUTION = rep(900, 444)
RETURN = runif(444)
CONTRIBUTION_RETURN = NET_CONTRIBUTION * (1+RETURN)

Then, you probably would not want to use matrix for the combination of these vectors. A data.frame seems more appropriate. Simply do:

MyDataFrame = data.frame(NET_CONTRIBUTION, RETURN, CONTRIBUTION_RETURN)
vanao veneri
  • 970
  • 2
  • 12
  • 31
  • 1
    While probably true, this is more of an opinion and does not address the error. – dario Feb 09 '20 at 15:39
  • Thank you. As for the concept of vectorization, I understand it, but though that because the the OPEN and CLOSE vectors are dependent on the results of the other vectors, It was better to do it in a matrix or data frame, as suggested. – TomTr Feb 09 '20 at 19:31