lapply() का उपयोग
lapply एक list के हर item पर एक function चलाता है और हमेशा एक list वापस देता है, हर बच्चे को same task देने और सारे results collect करने की तरह। यह आपके लिए repeating करता है।
In this page:
Syntax
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()
numbers <- list(1, 2, 3, 4)
squared <- lapply(numbers, function(x) x^2)
print(squared)
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. #}
आम गलतियां
lapplyसे एक vector वापस पाने की उम्मीद करना, जब यह हमेशा एक list return करता है; एक vector के लिएsapplyयाunlist()इस्तेमाल करें।- parentheses के साथ function लिखना जैसे
lapply(x, sqrt()), जो इसे pass करने की बजाय call करता है। - एक data frame पर
lapplyइस्तेमाल करना और यह भूल जाना कि यह columns पर loop करता है, rows पर नहीं।