[R] expanded dataset and random number

Muhammad Subianto msubianto at gmail.com
Wed Jun 21 17:22:03 CEST 2006


Dear all,
Per Jensen, thanks for your great help. All methods are very useful.

Best, Muhammad Subianto

On this day 20/06/2006 22:28, Per Jensen wrote:

> A couple of suggestions:
>
> #First solution
> mydatexpanded<-mydat[rep(1:5,mydat[,1]),]
>
> sampledat<-mydatexpanded[sample(1:85,7),-1]
>
> #Second solution
>
> sampledat<-mydat[sample(1:5,size=7,prob=mydat[,1]/85,replace=TRUE),-1]
>
> Regards
> Per Jensen
>
> On 6/20/06, Muhammad Subianto <msubianto at gmail.com> wrote:
>
>>
>> Dear all R-users,
>> (My apologies if this subject is wrong)
>> I have dataset:
>> mydat <- as.data.frame(
>>          matrix(c(14,0,1,0,1,1,
>>                   25,1,1,0,1,1,
>>                   5,0,0,1,1,0,
>>                   31,1,1,1,1,1,
>>                   10,0,0,0,0,1),
>>          nrow=5,ncol=6,byrow=TRUE))
>> dimnames(mydat)[[2]] <-c("size","A","B","C","D","E")
>> > mydat
>>   size A B C D E
>> 1   14 0 1 0 1 1
>> 2   25 1 1 0 1 1
>> 3    5 0 0 1 1 0
>> 4   31 1 1 1 1 1
>> 5   10 0 0 0 0 1
>> > sum(mydat$size)
>> [1] 85
>> >
>>
>> where size is number of each row that have this combination of 
>> variables.
>> In this dataset I have 85 tuples in expanded dataset.
>> I want to generate random number between 1 and 85.
>> Say, if the first random number is 15, so the number 15 is
>> 1 1 0 1 1
>> then, if the next random number is 7, then
>> 0 1 0 1 1
>>
>> then if
>> random number is 79
>> 0 0 0 0 1
>> random number is 46
>> 1 1 1 1 1
>> random number is 3
>> 0 1 0 1 1
>> random number is 28
>> 1 1 0 1 1
>>
>> So, the result random tuples (order from 6 random number):
>> 0 1 0 1 1
>> 0 1 0 1 1
>> 1 1 0 1 1
>> 1 1 0 1 1
>> 1 1 1 1 1
>> 0 0 0 0 1
>>
>> I  would be very happy if anyone could help me.
>> Thank you very much in advance.
>> Kindly regards,  Muhammad Subianto
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>



More information about the R-help mailing list