apply() का उपयोग
apply एक table की हर row या हर column के आर-पार एक function चलाता है, हर student की scores की row total करने वाली एक teacher की तरह। आप rows या columns चुनते हैं और tool बाकी करता है।
In this page:
Syntax
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()
m <- matrix(1:6, nrow = 2)
row_totals <- apply(m, 1, sum)
col_totals <- apply(m, 2, sum)
print(row_totals)
print(col_totals)
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. #}
आम गलतियां
MARGINswap करना, जब columns intended हों1और rows intended हों2इस्तेमाल करना।- text और numbers वाली एक data frame पर
apply()इस्तेमाल करना, जो सब कुछ characters में convert कर देता है। - यह उम्मीद करना कि
apply()हमेशा एक vector return करेगा, जब यह function के हिसाब से एक matrix या list return कर सकता है।