[R] Create a new variable and concatenation inside a "for" loop

David Winsemius dwinsemius at comcast.net
Thu Apr 28 22:27:12 CEST 2016

> On Apr 28, 2016, at 4:51 AM, Gordon, Fabiana <fabiana.gordon at imperial.ac.uk> wrote:
> Maybe I wasn't  clear  about my query. 
> I'm very familiar with pre-allocation and vectorization and I had already wrote an R code for this problem in this way. My question wasn't about the most efficient way to solve the problem. It was about whether in R it was possible to use the same  index used in the loop to create a new variable and store the results in as in the example showed below. The use of “c” was because I was using Matlab, otherwise I know that a new variable shouldn’t have the same name as the name of a function.
> Regards,
> Fabiana

>> On April 27, 2016 3:25:14 PM GMT+01:00, "Gordon, Fabiana" <fabiana.gordon at imperial.ac.uk> wrote:
>>> Hello,
>>> Suppose the you need a loop to create a new variable , i.e., you are 
>>> not reading data from outside the loop. This is a simple example in 
>>> Matlab code,
>>> for i=1:5
>>> r1=randn
>>> r2=randn
>>> r=[r1 r2]
>>> c(i,:)=r;   % creation of each row of c , % the ":" symbol indicates
>>> all columns. In R this would be [i,]
>>> end

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 {
  r= c(r1, r2)  # c() being used as the concatenation function
  c[ i, ] =r 

>>> The output of interest is c which I'm creating inside the "for" loop 
>>> -also the index used in the loop is used to create c. In R I had to 
>>> create c as an  empty vector (numeric() ) outside the loop, otherwise 
>>> I get an error message saying that c doesn't exit.
>>> The other issue is the concatenation. In each iteration I'm creating 
>>> the rows of c by placing the new row  (r) below the previous one so 
>>> that c becomes a 5 x 2 matrix.
>>> In R, it seems that I have no choice but use the function "rbind".

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 "["
>>> I 
>>> managed to write this code in R . However, I'm not sure that if 
>>> instead of creating a new variable  using  the index in the "for" loop 
>>> , I wanted to use the index to read data, e.g.  suppose I have a 2 X 
>>> 10 matrix X and suppose I want to calculate the sin () for each 2 x 2 
>>> sub-matrix of and stored in a matrix A. Then the code would be 
>>> something like this,

There's an array class in R that would allow addressing 2 x 2 slices.

>>> for i=1:5
>>> A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i))   % the ":" symbol indicates all
>>> rows
>>> end

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)

>>> Many Thanks,
>>> Fabiana
>>> Dr Fabiana Gordon
>>> Senior Statistical Consultant
>>> Statistical Advisory Service, School Of Public Health, Imperial 
>>> College London 1st Floor, Stadium House, 68 Wood Lane, London W12 7RH.


David Winsemius
Alameda, CA, USA

More information about the R-help mailing list