Vector की Arithmetic
Vector arithmetic एक साथ एक list के हर item पर math करता है, एक पूरी class को उनके scores में दो add करने को कहने की तरह। आपको हर एक अलग से touch करने की ज़रूरत नहीं।
In this page:
Syntax
result <- vector1 + vector2
result <- vector * number
Vector Arithmetic
Vectors पर Arithmetic operators automatically element-wise काम करते हैं -- c(1,2,3) + c(10,20,30) बिना एक loop के corresponding elements add करता है।
अगर vectors अलग lengths के हों, R छोटे वाले को recycle करता है, इसकी values repeat करते हुए लंबे vector की length match करने के लिए। यह vectorized behavior efficient, idiomatic R code लिखने में central है।
Note:
Vectorized operations R में हर element पर एक explicit loop लिखने से लगभग हमेशा तेज़ होते हैं।
उदाहरण: Vector Arithmetic
a <- c(1, 2, 3)
b <- c(10, 20, 30)
print(a + b)
print(a * 2)
# Recycling: shorter vector repeats to match length
print(c(1, 2, 3, 4) + c(10, 20))
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. #}
आम गलतियां
- अलग lengths वाले vectors जोड़ना जो evenly divide न हों, जैसे
c(1,2,3) + c(1,2), जो सिर्फ एक warning के साथ recycle करता है। - यह उम्मीद करना कि
*एक matrix product होगा, जब यह element by element multiply करता है; matrices के लिए%*%इस्तेमाल करें। - यह मान लेना कि recycling एक error देती है, जब R silently छोटा vector repeat करता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: