Skip to content
Prev 360515 / 398503 Next

Create a new variable and concatenation inside a "for" loop

It is possible. 

If the object `c` exists then you can address and assign in the manner you request and in this case we would assume that `c` has 2 columns


for i=1:5 {
  r1=rnorm(1)
  r2=rnorm(1)
  r= c(r1, r2)  # c() being used as the concatenation function
  c[ i, ] =r 
           }
If you are "adding" rows then you do need to use rbind. If you predimension which would always be faster in the long run, then you use "["
There's an array class in R that would allow addressing 2 x 2 slices.
Since `sin` is vectorized (in the R meaning of the term) and A is either a matrix or an array, you could just do this:

A <- sin(X)
David Winsemius
Alameda, CA, USA