Dear list

Suppose I have the following vector:

x <-  c(3,4,2,5,6)

Obviously, this sums to 20. Now, I want to have a second vector, call it
x2, that sums to x where 5 <= x <= 20, but there are constraints.

1) The new vector must be same length as x
2) No element of the new vector can be 0
3) Element x2[i] of the new vector cannot be larger than element x[i] of
the original vector
4) Ordering is not important

The following would be an example of what I would want if the user
wanted the vector x2 to sum to 19

x2 <- c(2,4,2,5,6) 

Or, because ordering is not important, this is acceptable

x2 <- c(3,3,2,5,6) 

Whereas this would not be appropriate

x3 <- c(4, 2,2,5,6)

Because element x3[1] is larger than x[1] even though it sums to 19.

Ideally, the function would take as input the original vector, x, and
the number that the new vector would sum to. In this example, the vector
could sum to any number 5 through 20.

For example,

myFun <- function(x, sumto) ... details ...

Is there a preexisiting function that would already do this? I have
spent too much (unsuccessful) time trying to write a function of my own
but can't seem to get this to work properly.

Any hints would be greatly appreciated.

Harold




        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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