Reputation: 3393
this might be quiet a strange question but...
I have 3 vectors:
myseq=seq(8,22,1)
myseqema3=seq(3,4,1)
myseqema15=seq(10,20,1)
And I want to assign the results to my list:
SLResultsloop=vector(mode="list")
With this loop:
for (i in myseq){
for(j in myseqema3){
for( k in myseqema15){
SLResultsloop[[i-7]]= StopLoss(data=mydata,n=i,EMA3=j,EMA15=k)
names(SLResultsloop[[i-7]])=rep(paste("RSI=",i,"EMA3=",j,"EMA15=",k,sep="|"),
length=length(SLResultsloop[[i-7]]))
}
}
}
The problem is as follows: the loop above overrides the list elements. So does any one have a clever solution about how to assign the loopresults to unique list elements (without overriding previous results)?
One solution could be to assign the output to different lists but it is a bit of an ugly solution...
Best Regards
Upvotes: 0
Views: 224
Reputation: 44575
You can skip the loops entirely by using expand.grid
and apply
(or something similar):
g <-
expand.grid(myseq = myseq,
myseqema3 = myseqema3,
myseqema15 = myseqema15)
apply(g, 1, function(a) {
StopLoss(data=mydata, n=a[1], EMA3=a[2], EMA15=a[3])
})
You can then build your names for each element of the return value from apply
using something like:
paste("RSI=",g[,1], "EMA3=", g[,2],"EMA15=", g[,3], sep="|")
Upvotes: 1