Re: [R] Sampe numbers

2016-07-19 Thread Daniel Nordlund

Frederic,

you need to be more clear about what you want to do.  If you are 
sampling 100 numbers from 1:100 with replacement, the sum will always be 
greater than 50 (as has already been pointed out).


In addition, you mention trying to eliminate zeros.  If you are sampling 
from 1:100 there will be no zeros.  So what are you really trying to do?



Dan

Dan Nordlund
Port Townsend, WA  USA


On 7/19/2016 11:54 AM, Frederic Ntirenganya wrote:

The problem is that there
are some zeros  while the numbers should range from 1 to  100

As the replace= TRUE if the smallest candidate is 1 may be replaced
because if I can have 1, fifty times this is equal to 50.
Remember that it is a sampling with a replacement

I am still wondering how I can remove the zero value.


Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

On Tue, Jul 19, 2016 at 9:11 PM,  wrote:


N <- 100
C <- 50
x <- numeric(N)
for (i in 1:N){
  x[i] <- sample(C-sum(x),1)
}
x
sum(x)




*Frederic Ntirenganya >*
Sent by: "R-help" 

07/19/2016 01:41 PM
To
"r-help@r-project.org" ,

cc
Subject
[R] Sampe numbers




Hi Guys,
I am trying to sample 100 numbers from 1:100
i did it  like this:

sample(1:100,100, replace= TRUE)
but i want again include a constraint that their sum must be equal to 50

How could I do it?

Cheers

Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.




[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.




--
Daniel Noredlund
Bothell, WA USA

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Sampe numbers

2016-07-19 Thread Frederic Ntirenganya
The problem is that there
are some zeros  while the numbers should range from 1 to  100

As the replace= TRUE if the smallest candidate is 1 may be replaced
because if I can have 1, fifty times this is equal to 50.
Remember that it is a sampling with a replacement

I am still wondering how I can remove the zero value.


Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

On Tue, Jul 19, 2016 at 9:11 PM,  wrote:

> N <- 100
> C <- 50
> x <- numeric(N)
> for (i in 1:N){
>   x[i] <- sample(C-sum(x),1)
> }
> x
> sum(x)
>
>
>
>
> *Frederic Ntirenganya >*
> Sent by: "R-help" 
>
> 07/19/2016 01:41 PM
> To
> "r-help@r-project.org" ,
>
> cc
> Subject
> [R] Sampe numbers
>
>
>
>
> Hi Guys,
> I am trying to sample 100 numbers from 1:100
> i did it  like this:
>
> sample(1:100,100, replace= TRUE)
> but i want again include a constraint that their sum must be equal to 50
>
> How could I do it?
>
> Cheers
>
> Frederic Ntirenganya
> Maseno University,
> African Maths Initiative,
> Kenya.
> Mobile:(+254)718492836
> Email: fr...@aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Sampe numbers

2016-07-19 Thread JLucke
N <- 100
C <- 50
x <- numeric(N)
for (i in 1:N){
  x[i] <- sample(C-sum(x),1)
}
x
sum(x)





Frederic Ntirenganya  
Sent by: "R-help" 
07/19/2016 01:41 PM

To
"r-help@r-project.org" , 
cc

Subject
[R] Sampe numbers






Hi Guys,
I am trying to sample 100 numbers from 1:100
i did it  like this:

sample(1:100,100, replace= TRUE)
but i want again include a constraint that their sum must be equal to 50

How could I do it?

Cheers

Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

 [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Sampe numbers

2016-07-19 Thread Greg Snow
I think that you need to reconsider your conditions.

The smallest number in your candidate set is 1, so if you sample 100
1's they will add to 100 which is greater than 50.  So to have a set
of numbers that sums to 50 you will need to either include negative
numbers, 0's, or sample fewer than 50 values.

If you really meant sum to 500 or sample 10 numbers (still selecting
any single number over 50 will make the sum to large) then this
becomes possible.  There are a few ways to do it, which is best
depends on what you are really trying to accomplish (and note that
with the constraint, the values will not be iid).

One option is rejection sampling:  take a sample, if it sums correctly
then you are done, if not then throw out that sample and try a new
one.  This will be very inefficient, but is easy enough to code and
with a fast enough computer may be acceptable.

Another option is adjustment:  take a sample and compute the sum, if
it is to low then add the difference to one of your values (lowest,
chosen at random, other) or split it up and add to multiple values.
If the sum is too high, then subtract from your numbers until the sum
is correct (checking that you don't go below any lower bounds).

Another option is to round values generated from a Dirichlet
distribution multiplied by your total (you may need to round one value
differently than the default).

Another option is to consider it a balls and urns problem:  You have
500 balls (assuming the true sum is to be 500) that you want
distributed into 100 urns.  If each urn needs at least 1 ball (minimum
value of 1) then put 1 ball in each urn, then for the remaining 400
balls, randomly choose an urn to put each into.  Then count the number
of balls in each urn and those are your 100 numbers that sum to 500
(as long as no balls bounced off the lip of the urn and rolled under
the couch).

There are probably other ways, but these are some things to get you started.

On Tue, Jul 19, 2016 at 11:41 AM, Frederic Ntirenganya
 wrote:
> Hi Guys,
> I am trying to sample 100 numbers from 1:100
> i did it  like this:
>
> sample(1:100,100, replace= TRUE)
> but i want again include a constraint that their sum must be equal to 50
>
> How could I do it?
>
> Cheers
>
> Frederic Ntirenganya
> Maseno University,
> African Maths Initiative,
> Kenya.
> Mobile:(+254)718492836
> Email: fr...@aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.