← Back to R Course | Chapter 11: Apply Family | Lesson 1 of 6

apply() का उपयोग

apply एक table की हर row या हर column के आर-पार एक function चलाता है, हर student की scores की row total करने वाली एक teacher की तरह। आप rows या columns चुनते हैं और tool बाकी करता है।

In this page:

  1. apply()
Syntax
r
apply(X, MARGIN, FUN)    # MARGIN: 1 = rows, 2 = columns

apply()

apply(X, MARGIN, FUN) एक matrix या data frame की rows (MARGIN = 1) या columns (MARGIN = 2) के आर-पार एक function apply करता है, एक manual loop की जगह एक single vectorized call से लेते हुए।

यह results का एक vector (या matrix) return करता है, process हुई प्रति row या column एक। जब आपका data matrix-shaped हो और इसे एक dimension के along same operation चाहिए यह go-to function है।

Note:
  • MARGIN = 1 मतलब "apply by row"।
  • MARGIN = 2 मतलब "apply by column" -- confusion का एक common source जिसे memorize करना चाहिए।

उदाहरण: apply()

markup
m <- matrix(1:6, nrow = 2)

row_totals <- apply(m, 1, sum)
col_totals <- apply(m, 2, sum)

print(row_totals)
print(col_totals)
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. MARGIN swap करना, जब columns intended हों 1 और rows intended हों 2 इस्तेमाल करना।
  2. text और numbers वाली एक data frame पर apply() इस्तेमाल करना, जो सब कुछ characters में convert कर देता है।
  3. यह उम्मीद करना कि apply() हमेशा एक vector return करेगा, जब यह function के हिसाब से एक matrix या list return कर सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.