← Back to R Course | Chapter 3: Vectors | Lesson 3 of 7

Vector की Arithmetic

Vector arithmetic एक साथ एक list के हर item पर math करता है, एक पूरी class को उनके scores में दो add करने को कहने की तरह। आपको हर एक अलग से touch करने की ज़रूरत नहीं।

In this page:

  1. Vector Arithmetic
Syntax
r
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

markup
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))
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. अलग lengths वाले vectors जोड़ना जो evenly divide न हों, जैसे c(1,2,3) + c(1,2), जो सिर्फ एक warning के साथ recycle करता है।
  2. यह उम्मीद करना कि * एक matrix product होगा, जब यह element by element multiply करता है; matrices के लिए %*% इस्तेमाल करें।
  3. यह मान लेना कि recycling एक error देती है, जब R silently छोटा vector repeat करता है।
🔒

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.