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@r-project.org mailing list
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.

Reply via email to