# [R] Repeating a series of commands

Bert Gunter gunter.berton at gene.com
Thu Oct 11 20:45:39 CEST 2012

```encapsulate them into a function and call the function ??

-- Bert

On Thu, Oct 11, 2012 at 11:09 AM, KoopaTrooper <ncooper1 at tulane.edu> wrote:
> I'm trying to figure out how to repeat a series of commands in R and have the
> outputs added to a dataframe after each iteration.
>
> My code starts this way...
>
>
> a\$Z<-ifelse(a\$Z=="L",sample(1:4,length(a\$Z),replace=TRUE),ifelse(a\$Z=="M",sample(5:8,length(a\$Z),replace=TRUE),ifelse(a\$Z=="U",sample(9:10,length(a\$Z),replace=TRUE),"")))
> a\$Z<-as.numeric(a\$Z)
> b\$Z<-ifelse(b\$Z=="L",sample(1:4,length(b\$Z),replace=TRUE),ifelse(b\$Z=="M",sample(5:8,length(b\$Z),replace=TRUE),ifelse(b\$Z=="U",sample(9:10,length(b\$Z),replace=TRUE),"")))
> b\$Z<-as.numeric(b\$Z)
>
> This is basically just starting off with a new and partially random data set
> every time that then goes through a bunch of other commands (not shown) and
> ends with the following outputs saved.
>
> Output1, Output2, Output3, Output4
>
> where each of these is just a single number. My questions is:
>
> 1. How do I repeat the entire series of commands x number of times and save
> each of the outputs into a structure like this:
>                       Output1      Output2     Output3     Output4
> Iteration 1
> Iteration 2
> Iteration 3
> etc.
>
> Not even sure where to start. Are loops the answer? Thanks,
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Repeating-a-series-of-commands-tp4645881.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website: