subset() का उपयोग
subset एक table की सिर्फ वे rows pick करता है जो एक rule satisfy करती हैं, एक color के सारे cards निकालने की तरह। आप rule describe करते हैं और एक छोटी table पाते हैं।
In this page:
Syntax
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()
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)
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. #}
आम गलतियां
- column name quotes में रखना, जैसे
subset(df, "age" >= 30), जो text को एक number से compare करता है। - functions या packages के अंदर
subset()इस्तेमाल करना, जहाँ variables की lookups unexpectedly behave कर सकती हैं, जबdf[df$age >= 30, ]safer है। - conditions combine करने के लिए
&की बजाय&&इस्तेमाल करना, जो पूरे columns पर काम नहीं करता।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: