# [R] Help on a combinatorial task (lists?)

jim holtman jholtman at gmail.com
Tue Aug 11 18:08:17 CEST 2009

```Does 'unlist' do it for you:

> w <- c(3,3,2,1)  # vector of weights
> q <- 4  # theshold
>
> # computes which coordinate of w is decisive in each permutation
> res <- unlist(sapply( permn(w), function(x) which(w == x[min(which(cumsum(x) >=q))]) ))
>
> # complies the frequencies
> prop.table( tabulate( res ))
[1] 0.4 0.4 0.1 0.1
>

On Tue, Aug 11, 2009 at 7:03 AM, Serguei
Kaniovski<Serguei.Kaniovski at wifo.ac.at> wrote:
> Hello!
> I have the following combinatorial problem.
> Consider the cumulative sums of all permutations of a given weight vector
> 'w'. I need to know how often weight in a certain position brings the
> cumulative sums equal or above the given threshold 'q'. In other words,
> how often each weight is decisive in raising the cumulative sum above 'q'?
>
> Here is what I do:
>
> w <- c(3,2,1)  # vector of weights
> q <- 4  # theshold
>
> # computes which coordinate of w is decisive in each permutation
> res <- sapply( permn(w), function(x) which(w == x[min(which(cumsum(x) >=
> q))]) )
>
> # complies the frequencies
> prop.table( tabulate( res ))
>
>
> The problem I have is that when the weights are not unique, the which()
> function returns a list as opposed to a vector. I don’t know how to
> proceed when this happens, as tabulate does not work on lists.
>
> The answer, of course, should be that equal weights are “decisive” equally
> often.
>
>
> Can you help?
> Thanks a lot!
>
> Serguei Kaniovski
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

```