sample() का उपयोग
sample एक list से randomly items pick करता है, एक hat से names निकालने की तरह। आप बताते हैं कितने draw करने हैं।
In this page:
Syntax
sample(x, size)
sample(x, size, replace = TRUE)
sample()
sample(x, size) randomly size elements एक vector x से draw करता है, default से बिना replacement के -- repeats allow करने के लिए replace = TRUE pass करें।
यह random sampling, shuffling, और simulation के लिए इस्तेमाल होता है। set.seed() के साथ combine होकर, sampling results reproducible बन जाते हैं।
Note:
बिना size argument के sample(x), और size, length(x) के बराबर, effectively पूरा vector shuffle करता है।
उदाहरण: sample()
set.seed(42)
values <- 1:10
drawn <- sample(values, 3)
print(drawn)
shuffled <- sample(values)
print(shuffled)
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- बिना
replace = TRUEके exist करने वाले से ज़्यादा items sample करना, जैसेsample(1:10, 20), जो एक error देता है। - एक single number पर
sample(5)call करना और1:5का एक shuffle पाना, जब यह intended न हो। - हर run में same result की उम्मीद करना, जब reproducibility के लिए आपको
set.seed()चाहिए।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: