[R] Repeating a series of commands
Jessica Streicher
j.streicher at micromata.de
Fri Oct 12 12:02:36 CEST 2012
This is pretty confusing writeup, but if you have iterations, then yes, you need a loop or a recursion.
A loop is probably easier, take a look at
?"for"
or
?apply
for this.
there are several derivates of apply
for example, say your outputs are what the code below gives you for each file, then first make a vector with the files
filevec<-c("f1.csv","f2.csv")
then you can easily get the "results" (a$z,b$z) as another vector by using
outputs <- sapply(filevec,function(x){
contents<-read.csv(x);
something<- ifelse(
x$Z=="L",sample(1:4,length(x$Z),replace=TRUE),
ifelse(x$Z=="M",sample(5:8,length(x$Z),replace=TRUE),
ifelse(x$Z=="U",sample(9:10,length(x$Z),replace=TRUE),"")));
return(something)
})
and if you want to do this several times:
m<- matrix(ncol=length(filevec),nrow=10)
myResult<- as.data.frame(m)
for(i in 1:10){
outputs <- sapply(filevec,function(x){
contents<-read.csv(x);
something<- ifelse(
contents$Z=="L",sample(1:4,length(contents$Z),replace=TRUE),
ifelse(contents$Z=="M",sample(5:8,length(contents$Z),replace=TRUE),
ifelse(contents$Z=="U",sample(9:10,length(contents$Z),replace=TRUE),"")));
return(something)
})
someStructure[i,]<-outputs
}
If you don't know the number of iterations or outputs beforehand, i'd have a look at "while" and use lists.
Hope this helps.
On 11.10.2012, at 20:09, KoopaTrooper 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<-read.csv("File1.csv")
> b<-read.csv("File2.csv")
>
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list