David Winsemius wrote:>
> On Dec 27, 2009, at 11:17 AM, Cat Morning wrote:
>
>> Hi all,
>>
>> I have made a loop function which prints outputs. For example:
>>
>> for (i in 1:5)
>> + print(i)
>> [1] 1
>> [1] 2
>> [1] 3
>> [1] 4
>> [1] 5
>>
>> However, I would like the output to given as a list or a vector, e..g
>> c(1,2,3,4,5). Is it possible to do this?
>
> This does suggest that you need to do some more studying of whatever
> text you are using to learn R:
>
> > v <- c()
> > for (i in 1:5) v <- c(v,i)
> > v
> [1] 1 2 3 4 5
>
> Or if you wanted to see the results with each iteration:
> > v <- c()
> > for (i in 1:5) print(v <- c(v,i))
> [1] 1
> [1] 1 2
> [1] 1 2 3
> [1] 1 2 3 4
> [1] 1 2 3 4 5
>>
>> (The function I am using outputs many 1000s of results, so is not easy
>> to change to a vector manually.)
Completely right, but just for completeness let me add that the loop is
slow using your allocation method (add more memory to v in each
iteration), better allocate the whole vector v at first as in:
v <- numeric(5)
for (i in 1:5) print(v[i] <- i)
well, given your really need a loop ...
Best,
Uwe Ligges
> So you probably would not want the print() version.
>> Many Thanks.
>>
>> [[alternative HTML version deleted]]
>
> Set your mail-client up to send plain-text.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> R-help at 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.