← Back to R Course | Chapter 8: Data Manipulation | Lesson 3 of 7

subset() का उपयोग

subset एक table की सिर्फ वे rows pick करता है जो एक rule satisfy करती हैं, एक color के सारे cards निकालने की तरह। आप rule describe करते हैं और एक छोटी table पाते हैं।

In this page:

  1. subset()
Syntax
r
subset(df, condition)
subset(df, condition, select = c(column1, column2))

subset()

subset(df, condition) एक logical condition के आधार पर एक data frame की rows filter करता है, एक SQL WHERE clause जैसा। यह एक select argument भी accept करता है एक साथ specific columns choose करने के लिए।

subset() ज़्यादा verbose df[df$col > x, ] indexing syntax पर एक convenient shorthand है।

Note: subset() की condition column names को सीधे reference कर सकती है, बिना df$ prefixes चाहिए, standard bracket indexing के उलट।

उदाहरण: subset()

markup
df <- data.frame(name = c("Alice", "Bob", "Cara"), age = c(30, 25, 40))

adults <- subset(df, age >= 30)
print(adults)

names_only <- subset(df, age >= 30, select = name)
print(names_only)
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. column name quotes में रखना, जैसे subset(df, "age" >= 30), जो text को एक number से compare करता है।
  2. functions या packages के अंदर subset() इस्तेमाल करना, जहाँ variables की lookups unexpectedly behave कर सकती हैं, जब df[df$age >= 30, ] safer है।
  3. conditions combine करने के लिए & की बजाय && इस्तेमाल करना, जो पूरे columns पर काम नहीं करता।
🔒

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.