[R] All the products of common factors

Ben Bolker bolker at ufl.edu
Tue Feb 24 15:18:59 CET 2009

Fox, Gordon <gfox <at> cas.usf.edu> writes:

> Example: 40 and 80 have these factors: c(1,2,2,2,5) and c(1,2,2,2,2,5).
> We can use match() to get the common factors c(1,2,2,2,5). What we want
> to be able to get from this is the list of all the possible products,
> which should be the concatenation of the original list, the products of
> all the pairs, of all the triplets, and so forth:
> c(1,2,2,2,5,4,8,10,20,40). 

  This is somewhat brute force, but how about

commfac <- c(1,2,2,2,5)
cfun <- function(i) { combn(commfac,i,prod) }
L <- lapply(as.list(1:length(commfac)),cfun)


  Ben Bolker

More information about the R-help mailing list