看板 R_Language 關於我們 聯絡資訊
※ 引述《siscom (妹控)》之銘言: : 大家好 : 最近在上程式語言的課 : 教授請我們跑bootstrap : 但不使用sample的function 而是自己寫function : 條件為 : Create a function call my.sample with three arguments: : x:the data to be resampled : n:number of data points to sample : Replacement:resample with replacement or not : Only ‘runif’ ‘order’ and ‘if else’ statements are allowed : 請問這要怎麼撰寫呢? : 想破頭還是想不出來 my.sample <- function(x, n, replace = FALSE) { if (!replace && length(x) < n) stop("The size must be less than the size of x when replace is TRUE.") if (replace) { prob <- 0:length(x)/length(x) return(x[findInterval(runif(n), prob)]) } else { return(x[order(runif(length(x)))][1:n]) } } findInterval如果不能用就自己用迴圈做一個吧XD -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.14.59.166 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1570962668.A.FA0.html ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:33:29 ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:34:14 ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:34:46