← Back to R Course | Chapter 9: Statistics | Lesson 5 of 7

sample() का उपयोग

sample एक list से randomly items pick करता है, एक hat से names निकालने की तरह। आप बताते हैं कितने draw करने हैं।

In this page:

  1. sample()
Syntax
r
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()

markup
set.seed(42)
values <- 1:10

drawn <- sample(values, 3)
print(drawn)

shuffled <- sample(values)
print(shuffled)
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. #}
आम गलतियां
  1. बिना replace = TRUE के exist करने वाले से ज़्यादा items sample करना, जैसे sample(1:10, 20), जो एक error देता है।
  2. एक single number पर sample(5) call करना और 1:5 का एक shuffle पाना, जब यह intended न हो।
  3. हर run में same result की उम्मीद करना, जब reproducibility के लिए आपको set.seed() चाहिए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.