← Back to R Course | Chapter 7: Data Structures | Lesson 2 of 7

Lists क्या हैं

एक list एक bag है जो किसी भी size की अलग-अलग तरह की चीज़ें hold कर सकता है, एक book, एक lunch, और एक ball वाले एक backpack की तरह। एक vector के उलट, इसके items match होने की ज़रूरत नहीं।

In this page:

  1. Lists
Syntax
r
list_name <- list(name1 = value1, name2 = value2)
list_name$name1
list_name[["name2"]]

Lists

एक list अलग types और अलग lengths के elements एक साथ hold कर सकता है, एक vector के उलट जिसे uniform type चाहिए।

Elements double brackets से access होते हैं, list[[i]], खुद value पाने के लिए, जबकि single brackets, list[i], एक sub-list return करते हैं। Lists R का flexible general-purpose container हैं, दूसरी languages में एक struct या heterogeneous array जैसा।

Warning:
  • list[1] पहला element रखने वाली एक list return करता है।
  • list[[1]] खुद element return करता है -- इन्हें mix up करना एक बहुत common bug है।

उदाहरण: Lists

markup
person <- list(name = "Alice", age = 30, active = TRUE)

print(person)
cat("Name:", person[["name"]], "\n")
cat("Name (dollar syntax):", person$name, "\n")
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. person["name"] इस्तेमाल करना और value की उम्मीद करना, जब single brackets एक list return करते हैं और [["name"]] या $name element return करते हैं।
  2. c() से values combine करना जब इनके अलग types हों, इसलिए ये list() में रहने की बजाय एक type में बदल जाते हैं।
  3. यह मान लेना कि एक list को arithmetic में एक vector की तरह इस्तेमाल किया जा सकता है, जैसे person + 1, जो एक 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.