# [R] for loop

Jim Lemon drjimlemon at gmail.com
Tue Jan 12 21:17:44 CET 2016

```Hi maryam (firoozi),
Apart from the fact that you are overworking your sires (or the more
realistic scenario of differential mating success) you can achieve the
700:30 ratio in this simple way:

sires<-paste("Sire",1:30,sep="")
dams<-paste("Dam",1:700,sep="")
ped<-data.frame(offspring=1:700,sire=sample(rep(sires,length.out=700),700),dams)

This mates your 30 sires randomly with the 700 dams. As Michael has pointed
out, each sire will get lucky 23.33 times on average. With just another 5
sires, you can get this down to 20. Of course if we want to adopt the
"anything goes" condition of a one in seven chance of parthenogenesis, try
this:

sires<-paste("Sire",1:30,sep="")
dams<-paste("Dam",1:700,sep="")
miracles<-paste("None",1:100,sep="")
ped<-data.frame(offspring=1:700,sire=sample(c(rep(sires,each=20),miracles),700),dams)

Jim

On Wed, Jan 13, 2016 at 4:02 AM, Michael Dewey <lists at dewey.myzen.co.uk>
wrote:

> Dear Maryam
>
> Please keep the list cc'ed in as others will have better answers than me.
>
> If dam has 700 members then sample (dam) gives you a random permutation of
> dams, each once.
>
> I did not understand the second part as i do not think you can have 30
> sires each occurring 20 times. Did you mean 35 sires? You can follow the
> same procedure as I suggested to do that.
>
> I think you perhaps need to re-read some introductory material as you are
> struggling over some fairly basic concepts here.
>
> Michael.
>
> On 12/01/2016 16:54, MARYAM wrote:
>
>> Dear Michael,
>> thanks a lot for your answering. I have question if i have 700 dam and 30
>> sire. how can i write my ped thad dam use only once and sire use 20 time or
>> 1:20 dam with sire 1 then 21:40 dam with sire 2 and …
>>
>>
>>
>> On Dey 22, 1394 AP, at 19:52, Michael Dewey <lists at dewey.myzen.co.uk>
>> wrote:
>>
>> Dear Maryam
>>>
>>> sample(dam) would give you a random permutation of dams
>>> sample(c(sire, sire)) would give you a random permutation of sires, each
>>> twice.
>>>
>>> Does that help?
>>>
>>> On 12/01/2016 05:53, maryam firoozi via R-help wrote:
>>>
>>>>
>>>> I want to mak a matrix with 10 row and 3 column . this matrix is
>>>> pedigree. my input
>>>> sire<- c(1,2,3,4,5)
>>>> count<- 0
>>>> sire<- cbind(sire,count)
>>>> dam<- c(1,2,3,4,5,6,7,8,9,10)
>>>> ped<-mstrix(NA,nrow=10,ncol=3)
>>>> for(i in 1:10){
>>>> Sire<- sample(sire[,1],1)
>>>> a<- which(sire[,1]==Sire)
>>>> if(a){sire[a,2]<-sire[a,2]+1}
>>>>
>>>> Dam<- sample(dam,1)
>>>> ped[i,1]<- 1:10
>>>> ped[i,2]<- Sire
>>>> ped[i,3]<- Dam}
>>>> i cant write a code that each sire use only twic not more in ped and
>>>> Dam use only once.
>>>> can you help me?
>>>> sincerely
>>>> firoozi
>>>> Sent from my iPhone
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>> --
>>> Michael
>>> http://www.dewey.myzen.co.uk/home.html
>>>
>>
>>
>>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help