[R] Managing output

Rolf Turner r.turner at auckland.ac.nz
Wed Aug 26 22:06:39 CEST 2009


On 27/08/2009, at 8:00 AM, Duncan Murdoch wrote:

> On 26/08/2009 3:20 PM, Noah Silverman wrote:
>> Hi,
>>
>>
>> Is there a way to build up a vector, item by item.  In perl, we can
>> "push" an item onto an array.  How can we can do this in R?
>> I have a loop that generates values as it goes.  I want to end up  
>> with a
>> vector of all the loop results.
>>
>> In perl it woud be:
>>
>> for(item in list){
>>      result <- 2*item^2 (Or whatever formula, this is just a  
>> pseudo example)
>>      Push(@result_list, result)  (This is the step I can't do in R)
>> }
>>
>>
>> Thanks!
>
> Use the c() function, e.g.
>
> result <- numeric(0)

	That should be result_list <- numeric(0)!!! :-)

		cheers,

			Rolf

> for (item in list) {
>    result <- 2*item^2
>    result_list <- c(result_list, result)
> }
>
> If the list is long, this is an extremely inefficient style of R
> programming, but for a short list it should be fine.
>
> Duncan Murdoch
>
> ______________________________________________
> 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.


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list