[R] Help on a combinatorial task (lists?)
Serguei Kaniovski
Serguei.Kaniovski at wifo.ac.at
Tue Aug 11 13:03:22 CEST 2009
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
More information about the R-help
mailing list