help with apply, please
On Saturday 19 November 2005 22:09, Gabor Grothendieck wrote:
Getting back to your original question of using apply, solving the LP
gives us the number of components in any minimal solution and
exhaustive search of all solutions with that many components can
be done using combinations from gtools and apply like this:
library(gtools) # needed for combinations
soln <- lp("min", rep(1,3), rbind(t(mtrx)), rep(">=", 4),
rep(1,4))$solution k <- sum(soln)
m <- nrow(mtrx)
combos <- combinations(m,k)
combos[apply(combos, 1, function(idx) all(colSums(mtrx[idx,]))),]
In the example we get:
[,1] [,2]
[1,] 1 3
[2,] 2 3
which says that rows 1 and 3 of mtrx form one solution
and rows 2 and 3 of mtrx form another solution.
I'm speechless. It is exactly what I needed. A billion of thanks! Adrian
Adrian DUSA
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
+40 21 3120210 / int.101