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

lapply() का उपयोग

lapply एक list के हर item पर एक function चलाता है और हमेशा एक list वापस देता है, हर बच्चे को same task देने और सारे results collect करने की तरह। यह आपके लिए repeating करता है।

In this page:

  1. lapply()
Syntax
r
result <- lapply(x, FUN)

lapply()

lapply(x, FUN) एक vector या list के हर element पर एक function apply करता है और हमेशा एक list return करता है, चाहे function खुद क्या return करे।

यह apply family का सबसे general member है, क्योंकि एक list result किसी भी type या length की values hold कर सकता है। जब आपको result को एक vector में simplify नहीं करना lapply() इस्तेमाल करें।

Note: lapply() हमेशा एक list return करता है -- जब आप चाहते हैं result automatically एक vector में simplify हो sapply() इस्तेमाल करें।

उदाहरण: lapply()

markup
numbers <- list(1, 2, 3, 4)

squared <- lapply(numbers, function(x) x^2)
print(squared)
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. lapply से एक vector वापस पाने की उम्मीद करना, जब यह हमेशा एक list return करता है; एक vector के लिए sapply या unlist() इस्तेमाल करें।
  2. parentheses के साथ function लिखना जैसे lapply(x, sqrt()), जो इसे pass करने की बजाय call करता है।
  3. एक data frame पर lapply इस्तेमाल करना और यह भूल जाना कि यह columns पर loop करता है, rows पर नहीं।
🔒

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.