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

MacQueen, Don macqueen1 at llnl.gov
Thu Apr 28 20:54:14 CEST 2016


What's not possible, as far as I know, is to create a variable using an
expression that refers to elements within the variable.

For example, suppose that a some point during my R session, there is no
variable named tmpx:

> exists('tmpx')
[1] FALSE

If I try to reference tmpx, in this case by using an extraction operator

> tmpx[3] <- 2
Error in tmpx[3] <- 2 : object 'tmpx' not found

it fails. That is, you can't assign to the third element of tmpx when tmpx
doesn't exist, and trying to do so does not automatically create tmpx. As
best as I can tell, this is what you are asking about.

Note that this behavior of the R language has nothing to do with looping
or using a loop index to create a variable, but rather the idea that one
can't reference a non-existant variable.

In this case, I think that the R way of doing things is just different
than the matlab way.


-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 4/28/16, 4:51 AM, "R-help on behalf of Gordon, Fabiana"
<r-help-bounces at r-project.org on behalf of 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
>
>
>-----Original Message-----
>From: Bert Gunter [mailto:bgunter.4567 at gmail.com]
>Sent: 27 April 2016 21:18
>To: Jeff Newmiller
>Cc: Gordon, Fabiana; r-help at R-project.org
>Subject: Re: [R] Create a new variable and concatenation inside a "for"
>loop
>
>...
>
>"(R is case sensitive, so "C" has no such problem)."
>
>Well, not quite. Try ?C
>
>To add to the previous comments, Dr. Gordon appears to need to do her/his
>homework and spend some time with an R tutorial or two before posting
>further here. There are many good ones on the web. Some recommendations
>can be found here:
>https://www.rstudio.com/online-learning/#R
>
>Cheers,
>Bert
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>On Wed, Apr 27, 2016 at 12:57 PM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us> wrote:
>> "c" an extremely commonly-used function. Functions are first-class
>>objects that occupy the same namespaces that variables do, so they can
>>obscure each other. In short, don't use variables called "c" (R is case
>>sensitive, so "C" has no such problem).
>>
>> Wherever possible, avoid incremental concatenation like the plague. If
>>you feel you must use it, at least concatenate in lists and then use
>>functions like unlist, do.call, or pre-allocate vectors or matrix-like
>>objects with unuseful values like NA and then overwrite each element in
>>the vector or matrix-type object in a loop like your first one.
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> 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
>>>
>>>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". 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,
>>>
>>>for i=1:5
>>>A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i))   % the ":" symbol indicates all
>>>rows
>>>end
>>>
>>>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.
>>>
>>>Tel: 020 7594 1749
>>>Email:
>>>fabiana.gordon at imperial.ac.uk<mailto:fabiana.gordon at imperial.ac.uk>
>>>Web:
>>>www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-adv
>>>ice-service/<http://www.imperial.ac.uk/research-and-innovation/support
>>>-for-staff/stats-advice-service/>
>>>
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>>______________________________________________
>>>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide
>>>http://www.R-project.org/posting-guide.html
>>>and provide commented, minimal, self-contained, reproducible code.
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list