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

Named Vectors क्या हैं

एक named vector हर item को इसका अपना label देता है, हर locker पर एक name tag की तरह। आप फिर एक value को इसकी position की बजाय इसके नाम से माँग सकते हैं।

In this page:

  1. Named Vectors
Syntax
r
vector_name <- c(name1 = value1, name2 = value2)
names(vector_name) <- c("name1", "name2")
vector_name["name1"]

Named Vectors

एक vector के elements के names हो सकते हैं, names() से assigned या सीधे c(a = 1, b = 2) से, आपको position की बजाय नाम से elements access करने देते हुए।

Named vectors एक simple lookup table की तरह behave करते हैं। Names vector की values के साथ एक attribute की तरह store होते हैं।

Note: Named vectors simple key-value data के लिए एक dictionary/hash map का एक lightweight alternative हैं।

उदाहरण: Named Vectors

markup
ages <- c(alice = 30, bob = 25, cara = 40)

print(ages)
cat("Bob's age:", ages["bob"], "\n")
print(names(ages))
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. बिना quotes के ages[bob] इस्तेमाल करना, जो एक variable bob ढूंढता है; ages["bob"] इस्तेमाल करें।
  2. यह उम्मीद करना कि ages["bob"] बस number return करेगा, जब यह एक named vector return करता है; नाम हटाने के लिए ages[["bob"]] इस्तेमाल करें।
  3. एक non-existent नाम add करना और एक error की उम्मीद करना, जब ages["dan"] NA देता है।
🔒

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.