[R] Variable number of loops
WRAY NICHOLAS
nicholas.wray at ntlworld.com
Fri May 15 22:40:33 CEST 2015
I am trying to build a programme which will work out the permutations of a
number of variables, eg a=0 to 1, b=0 to 1, and c=0 to 2, so permutations
would be (0,0,0), (1,0,0), (0,1,0)... etc In this case there would be 2 x
2x 3 = 12 permutations. If the number of variables are fixed it's easy to
loop round with nesting
However I don't have a fixed number of variables, so I have a variable
number of loops. I am trying to use a recursive function to do this and
have been building it up step-wise
I want to return a list of all the permutations at the end, but the
programme I have so ar doesn't return a full list, but just the first
element
2 things -- 1 I don't see why this is happening, and 2 is this the right
way to approach this problem? I cannot find find anything about this in R
on the net
recursfunc1<-function(xf,shiftvecf,vlistf){
if(xf<4){
xf<-xf+1
vlistf[[length(vlistf)+1]]<-shiftvec[xf]
#print(paste(xf,"and",shiftvec[xf]))
print(vlistf)
#print(shiftvec[xf])
xf<-recursfunc1(xf,shiftvecf,vlistf)}
return(vlistf)}
shiftvec<-c(2,1,1,0)
vlist<-list()
perm<-recursfunc1(xf=0,shiftvecf=shiftvec,vlistf=vlist)
perm
I want perm to return the elements of shiftvec as a list so that I can then
do all the permutations as the next stage, but it only returns the first
element of shiftvec
Thanks, Nick Wray
[[alternative HTML version deleted]]
More information about the R-help
mailing list