[R] Make the output (from a Loop function) a Vector

David Winsemius dwinsemius at comcast.net
Sun Dec 27 17:46:30 CET 2009


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.)
>
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




More information about the R-help mailing list