簡體   English   中英

使用樣本空間大小= 1的sample()

[英]Using sample() with sample space size = 1

我有一份我希望從中抽樣的日期列表。 有時樣本空間只是一個日期,例如樣本(“10/11/11”,1)。 日期存儲為chron對象,因此當我在樣本空間中只有一個日期(並且只有那時)時,樣本將其視為向量(1:date)。 樣本文檔指出了這一點:

 If ‘x’ has length 1, is numeric (in the sense of ‘is.numeric’) and
 ‘x >= 1’, sampling _via_ ‘sample’ takes place from ‘1:x’.  _Note_
 that this convenience feature may lead to undesired behaviour when
 ‘x’ is of varying length in calls such as ‘sample(x)’.  See the
 examples.

但我沒有看到禁用此功能的方法。 是否有一種解決方法或方法來阻止它將長度為1的對象視為數字?

sample文檔建議:

resample <- function(x, ...) x[sample.int(length(x), ...)]

我會將它包裝在if語句中,或將其包裝在另一個函數中。 例如:

mysample <-
function(x, size, replace=FALSE, prob=NULL)
{
  if(length(x)==1)
    return(rep(x, size))

  sample(x, size, replace, prob)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM