Named Vectors क्या हैं
एक named vector हर item को इसका अपना label देता है, हर locker पर एक name tag की तरह। आप फिर एक value को इसकी position की बजाय इसके नाम से माँग सकते हैं।
In this page:
Syntax
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
ages <- c(alice = 30, bob = 25, cara = 40)
print(ages)
cat("Bob's age:", ages["bob"], "\n")
print(names(ages))
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. #}
आम गलतियां
- बिना quotes के
ages[bob]इस्तेमाल करना, जो एक variablebobढूंढता है;ages["bob"]इस्तेमाल करें। - यह उम्मीद करना कि
ages["bob"]बस number return करेगा, जब यह एक named vector return करता है; नाम हटाने के लिएages[["bob"]]इस्तेमाल करें। - एक non-existent नाम add करना और एक error की उम्मीद करना, जब
ages["dan"]NAदेता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: