[R] Variable number of loops
Jim Lemon
drjimlemon at gmail.com
Sun May 17 01:19:06 CEST 2015
Hi all,
Given the number of help requests that involve
permutations/combinations, and the less than obvious naming of the
expand.grid function, perhaps adding an alias such as
"permute.elements" or "combine.elements" might ease the tasks of both
searchers and those offering help. Neither of the above names appear
to be used at present.
Jim
On Sun, May 17, 2015 at 5:54 AM, Bert Gunter <gunter.berton at gene.com> wrote:
> 1. Please always reply to the list unless there is a compelling reason
> to keep the discussion private. You will have a better chance of
> getting something useful that way.
>
> 2. I don't know what you mean by "I don't have a fixed number of
> variables." You have to specify at least the number of variables and
> how many levels each has in order to work out what you requested,
> which is **NOT** the number of permutations but the number of
> combinations AFAICS, which is exactly what expand.grid will give you.
>
> 3. Maybe what you're looking for is the ... arguments in function
> calls, which would be used along the lines of:
>
> myfun <- function( x,y,...)
> {
> ## some code
> combs <- expand.grid(...)
> ## some more code
> }
>
> Any good R tutorial will tell you about this if this is unfamiliar.
>
> 4. Another possibility might be to deliver a list of named variables
> as an argument and then use do.call, e.g.
>
> myfun <- (x,y, alist)
> {
> ## some code
> combs <- do.call(expand.grid, alist)
> ## some more code
> }
>
> ?do.call and/or a tutorial for details.
>
> 5. Otherwise, maybe someone else can figure out what you're looking for.
>
> Cheers,
> Bert
>
>
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Sat, May 16, 2015 at 11:16 AM, WRAY NICHOLAS
> <nicholas.wray at ntlworld.com> wrote:
>> I might be but doesn't expand.grid need a defined and listed number of
>> inputs? The problem I'm having is that the number of variables is not
>> fixed, so I'm not sure whether I can reference the variable number of
>> variables by using a vector -- haven't had time to try yet But thanks
>> anyway Nick Wray
>>
>> On 16 May 2015 at 14:28, Bert Gunter <gunter.berton at gene.com> wrote:
>>>
>>> Are you trying to reinvent ?expand.grid ?
>>>
>>> -- Bert
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>> (650) 467-7374
>>>
>>> "Data is not information. Information is not knowledge. And knowledge
>>> is certainly not wisdom."
>>> Clifford Stoll
>>>
>>>
>>>
>>>
>>> On Fri, May 15, 2015 at 1:40 PM, WRAY NICHOLAS
>>> <nicholas.wray at ntlworld.com> wrote:
>>> > 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]]
>>> >
>>> > ______________________________________________
>>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> > 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.
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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