→ celestialgod: Assume dat is the dataset 05/04 16:03
→ celestialgod: dat %>% split(.$department)%>%lapply(function(x) 05/04 16:07
→ celestialgod: x[sample(1:nrow(x), 2),]) %>% rbindlist(.) 05/04 16:07
→ celestialgod: department是dat中系所的變數 05/04 16:08
→ celestialgod: base::split, data.table::rbindlist,magrittr::%>% 05/04 16:08
→ celestialgod: 2是取樣的樣本數 05/04 16:09
→ celestialgod: 你也可以用dplyr group_by做 05/04 16:10
→ celestialgod: rbindlist(.) 可以用do.call(rbind, .)取代 05/04 16:10
推 psinqoo: celestialgod 回文~這樣看很花 05/04 16:59
→ celestialgod: 不想要因此再開一篇文章QQ 05/04 18:01
→ celestialgod: 才兩行程式而已 05/04 18:01
→ gsuper: 用 tapply() 和 sample() 找出大矩陣的 index 就好 05/13 11:33
→ gsuper: tapply(1:10000,groupFactor,funciton(s){sample(s,30)}) 05/13 11:36
→ celestialgod: 樓上,split可以接根據變數做切割,會方便很多 05/13 11:36
→ gsuper: 然後: 大矩陣[index,] 05/13 11:36
→ celestialgod: 其實會比tapply快。 05/13 11:36
→ gsuper: Mm....split 也行 05/13 11:39
→ gsuper: sapply(split(1:6,c(1,1,1,2,2,2)),sample,2) 這個感覺 05/13 11:41