← Back to R Course | Chapter 3: Vectors | Lesson 4 of 7

Vector की Indexing

Vector indexing एक list से items इनकी position से पिक करता है, और R एक से count करना शुरू करता है। यह एक shelf पर तीसरी book माँगने जैसा है।

In this page:

  1. Vector Indexing
Syntax
r
vector_name[index]
vector_name[c(index1, index2)]
vector_name[-index]
vector_name[start:end]

Vector Indexing

R vectors 0 से नहीं, 1 से index होते हैं, v[3] जैसे square brackets इस्तेमाल करते हुए। आप एक साथ कई elements निकालने के लिए positions के एक vector से भी index कर सकते हैं, जैसे v[c(1, 3)]।

v[-1] जैसे Negative indices उस position को select करने की बजाय exclude करते हैं।

Warning: R indexing 0 से नहीं, 1 से शुरू होती है -- v[1] पहला element है, ज़्यादातर दूसरी programming languages के उलट।

उदाहरण: Vector Indexing

markup
v <- c(10, 20, 30, 40, 50)

cat(v[1], "\n")
print(v[c(1, 3)])
print(v[-1])
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. पहले index की तरह 0 इस्तेमाल करना, जब v[0] एक empty vector return करता है और पहला element v[1] है।
  2. v[-1] इस्तेमाल करना और last element की उम्मीद करना, जब यह पहला हटाता है और बाकी return करता है।
  3. length से आगे index करना, जैसे v[10], जो NA return करता है error नहीं।
🔒

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.