[R] sample {base}

Xochitl CORMON Xochitl.Cormon at ifremer.fr
Wed Jun 5 17:40:01 CEST 2013



Thank you Sarah for this code, it's exactly what I wanted to reach.


Le 05/06/2013 16:49, Sarah Goslee a écrit :
> What about using instead
> size = min(5, length(Gpool$SampleNb[Gpool$LngtClas == LngtClas[[4]]&
> Gpool$SpCode == SpCode[[2]]])
>
> that would make sure your sample is either the size of the data or 5.
>
> Sarah
>
> On Wed, Jun 5, 2013 at 10:27 AM, Xochitl CORMON
> <Xochitl.Cormon at ifremer.fr>  wrote:
>> Hi all,
>>
>> I'm trying to randomly select sample numbers for length class groups (5 per
>> length class).
>>
>> For this I'm using a loop FOR and the function sample () and specified a
>> size for the sampling of 5. Unfortunately, one of the length class group
>> does not contain 5 individuals. For me is not a big deal as I have others
>> groups to complete. However it bothers me that the sampling function does
>> not select any individuals in this group generating the error below :
>>
>> Error in sample(Gpool2$SampleNb[Gpool2$LngtClas == LngtClas[[i]]&
>> Gpool2$SpCode ==  : can not take a sample larger than the population when
>> 'replace = FALSE'.
>>
>> I understand why this error message appears but I was wondering if there is
>> a way to select all the items present in the group even if it's not 5
>> (something like size =<  5).
>>
>> LngtClas<- list( "40_49", "50_59", "60_69", "70_")
>> SpCode<- list ("POLLVIR ", "MERLMER")
>>
>> a<- as.character(sample(Gpool$SampleNb[Gpool$LngtClas == LngtClas[[4]]&
>> Gpool$SpCode == SpCode[[2]]], size = 5, replace = FALSE))
>>
>> You can find enclosed my dataset,
>>
>> Thank you for the help,
>>
>> Xochitl C.
>



More information about the R-help mailing list