On Tue, Aug 24, 2010 at 8:45 AM, Maas James Dr (MED) <j.m...@uea.ac.uk> wrote:
> Thanks Bert, will have a look.  I'm originally a Fortran programmer so tend 
> to think in loops ... so yes expect it may be job for loops, just tried to 
> avoid it

 because several references say not to use loops in R.

-- Yes, an unfortunate misunderstanding. The references refer mostly
to the unnecessary use of loops at the R (interpreted) level -- which
also includes _apply type constructs -- vs inbuilt vectorization,
which are loops at the C level and so orders of magnitude faster and
cleaner code, to boot. An example would be what you might be doing
following the Fortran paradigm:

## warning BAAAD R code
myvec <- vector("numeric", 1E5)
for(i in 1:(1E5))myvec[i] <- sin(i)

versus

myvec <- sin( (1:(1E5))) ## because sin() is vectorized


But conventional use of looping (often better --cleaner code, data
structures-- in their list versions: lapply, mapply, tapply,...) at
the interpreted R level is perfectly apporpriate; the only caveat
being that they can apparently bog down if the number of loops is
"very" large, perhaps > 1E5 or more. I have never actually experienced
such problems, however, so can't really speak to them.

-- Bert


>
> I note your helpful comments about lists, will see if I can do it that way!
>
> Will check this out, thanks
>
> Jim
>
>
> ===============================
> Dr. Jim Maas
> Research Associate in Network Meta-Analysis
> School of Medicine, Health Policy and Practice
> CD Annex, Room 1.04
> University of East Anglia
> Norwich, UK
> NR4 7TJ
>
> +44 (0) 1603 591412
>
>
> -----Original Message-----
> From: Bert Gunter [mailto:gunter.ber...@gene.com]
> Sent: Tuesday, August 24, 2010 4:39 PM
> To: r.ookie
> Cc: Maas James Dr (MED); r-help@r-project.org
> Subject: Re: [R] multiple assignments ?
>
> None of this would work if the list is long. Isn't this an obvious
> task for a loop, explicit or implicit?
>
> e.g.
>
> for(i in 1:100)assign(paste("vec",i,sep=""), vector("integer",5))
>
> or probably better because it creates a list structure:
>
> ## warning, untested. You may have to fool with the syntax a bit:
>
> listofempties <- lapply(1:100, vector,mode="integer",length=5)
>
> ## you can name the components with names(listofempties) <-
> paste("vec",1:100,sep="")
>
> HOWEVER, I rather doubt that any of this is necessary: that is, it is
> rarely necessary or wise in R to first create empty objects and then
> populate them. Using lists and list operations usually allows both to
> be done more efficiently and conveniently in one step.
>
> --
> Bert Gunter
> Genentech Nonclinical Statistics
>
> On Tue, Aug 24, 2010 at 8:19 AM, r.ookie <r.oo...@live.com> wrote:
>> Do you mean something like this?
>>
>>>
>>  n <- 5
>>
>>>
>>  (vec1 <- matrix(rep(1, n)))
>>     [,1]
>> [1,]    1
>> [2,]    1
>> [3,]    1
>> [4,]    1
>> [5,]    1
>>
>>>
>>  (vec2 <- matrix(rep(2, n)))
>>     [,1]
>> [1,]    2
>> [2,]    2
>> [3,]    2
>> [4,]    2
>> [5,]    2
>>
>>>
>>  (vec3 <- matrix(rep(3, n)))
>>     [,1]
>> [1,]    3
>> [2,]    3
>> [3,]    3
>> [4,]    3
>> [5,]    3
>>
>>>
>>
>>
>>>
>>  (vec <- matrix(c(vec1, vec2, vec3)))
>>      [,1]
>>  [1,]    1
>>  [2,]    1
>>  [3,]    1
>>  [4,]    1
>>  [5,]    1
>>  [6,]    2
>>  [7,]    2
>>  [8,]    2
>>  [9,]    2
>> [10,]    2
>> [11,]    3
>> [12,]    3
>> [13,]    3
>> [14,]    3
>> [15,]    3
>>
>>>
>>
>> On Aug 24, 2010, at 4:58 AM, Maas James Dr (MED) wrote:
>>
>> Simple one, have read and googled, still no luck!
>>
>> I want to create several empty vectors all of the same length.
>>
>> I would like multiple empty vectors (vec1, vec2, vec3) and want to create 
>> them all in one line.
>>
>> I've tried
>>
>> vec1,vec2,vec3 <- vector(length=5)
>> and
>> c(vec1,vec2,vec3) <- vector(length=5)
>>
>> and several other attempts but nothing seems to work ... suggestions?
>>
>> Thanks
>>
>> Jim
>>
>> ===============================
>> Dr. Jim Maas
>> University of East Anglia
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> 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@r-project.org mailing list
>> 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@r-project.org mailing list
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.

Reply via email to