[R] Random numbers for a group

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Dec 15 14:57:54 CET 2009


On 15-Dec-09 13:35:36, Jorge Ivan Velez wrote:
> Dear Maithili,
> Here are two suggestions:
> 
># data set
> x <- read.table(textConnection("Name no_of_instances
> AAA   12
> AA    17
> A      0
> BBB   11
> BB     6
> B      0
> C      8
> D      3"), header = TRUE)
> closeAllConnections()
> x
> 
># option 1
> do.call(c,with(x, tapply(no_of_instances, Name, runif)))
> 
># option 2
> with(x, runif( sum(no_of_instances) ))
> 
> HTH,
> Jorge

And, to round off the above:

  names(x) <- rep(Name,times=no_of_instances)

(or similar). Then you can extract the random numbers for any Name.

Ted.


> On Tue, Dec 15, 2009 at 8:26 AM, Maithili Shiva <> wrote:
> 
>> Dear R helpers
>>
>> I have following table
>>
>> Name                 no_of_instances
>> AAA                             12
>> AA                               17
>> A                                  0
>> BBB                             11
>> BB                                6
>> B                                  0
>> C                                  8
>> D                                  3
>>
>> Now I need to generate the uniform random numbers (against the no. of
>> instances) and assign these numbers generated against the respective
>> names.
>> E.g. I need to generate 12 random numbers and assign these numbers
>> against
>> AAA.
>>
>> Individually I can generate them as
>>
>> AAA_no = c(runif(12))
>> AA_no  = c(runif(17))
>> ......
>>
>> and so on.
>>
>> And in the end I can club them as
>>
>> ran_nos = c(AAA_no, AA_no, ..........D_no)
>>
>> My problem is if there are say 1000  names, then it will be a
>> cumbersome
>> job to generate 1000 individual random number sets and then to combine
>> them
>> to form a single dataset.
>>
>> Is there any alternative to this?
>>
>> Thanking in advance
>>
>> With regards
>>
>> Maithili
>>
>>
>>
>>
>>      The INTERNET now has a personality. YOURS! See your Yahoo!
>>      Homepage.
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at 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.
>>
>>
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Dec-09                                       Time: 13:57:50
------------------------------ XFMail ------------------------------




More information about the R-help mailing list