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

apply() Family का परिचय

apply family tools का एक set है जो एक साथ कई items पर एक function चलाते हैं, एक teacher की तरह जो हर paper को same rules से mark करता है। यह lesson आपको इनकी पहली झलक देता है।

In this page:

  1. apply() Family (Preview)
Syntax
r
apply(matrix_name, MARGIN, FUN)
lapply(x, FUN)
sapply(x, FUN)

apply() Family (Preview)

R के apply family के functions (apply, lapply, sapply, और बाकी) एक explicit loop लिखे बिना एक data structure के elements के आर-पार एक function apply करते हैं, और एक बाद के chapter में depth में cover होते हैं।

apply(m, 1, sum) एक matrix की हर row के आर-पार sum apply करता है। यह preview dedicated Apply Family chapter गहराई में जाने से पहले concept introduce करता है।

Note: apply() का दूसरा argument, 1 या 2, control करता है कि function एक matrix के row से apply हो या column से।

उदाहरण: The apply() Family (Preview)

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

row_sums <- apply(m, 1, sum)
col_sums <- apply(m, 2, sum)

print(row_sums)
print(col_sums)
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. apply(m, 1, sum) rows के लिए और 2 columns के लिए गलत तरीके से इस्तेमाल करना, इसलिए results swap हो जाते हैं।
  2. mixed types वाले एक data frame पर apply इस्तेमाल करना, जो इसे एक character matrix में convert कर देता है।
  3. यह उम्मीद करना कि sapply और lapply same type देंगे, जब lapply हमेशा एक list return करता है।
चैप्टर सारांश
  • Matrices, arrays, lists, data frames, और factors R की main data structures हैं।
  • Data frames tabular data hold करते हैं, और lists mixed types hold कर सकते हैं।
  • which() positions ढूंढता है, और structures के आर-पार काम करने के लिए apply() functions preview किए गए।
🔒

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.