Menu
Coddy logo textTech

Vector Arithmetic & Recycling

Part of the Fundamentals section of Coddy's R journey — lesson 69 of 78.

When you perform arithmetic operations between two vectors of the same length, R applies the operation element by element:

a <- c(1, 2, 3)
b <- c(10, 20, 30)
print(a + b)

Output:

[1] 11 22 33

But what happens when the vectors have different lengths? R uses a behavior called recycling. The shorter vector is repeated to match the length of the longer one:

numbers <- c(1, 2, 3, 4, 5, 6)
multiplier <- c(10, 100)
print(numbers * multiplier)

Output:

[1]  10 200  30 400  50 600

Here, multiplier was recycled to become c(10, 100, 10, 100, 10, 100) before the multiplication. This is especially useful when operating with a single value:

prices <- c(10, 20, 30)
print(prices * 1.1)

Output:

[1] 11 22 33

The single value 1.1 is recycled to match the length of prices. Be cautious when the longer vector's length isn't a multiple of the shorter one, as R will still perform the operation but may produce a warning about partial recycling.

challenge icon

Challenge

Easy

You will receive two lines of input:

  1. A comma-separated list of numbers representing base values (e.g., 10,20,30,40,50,60)
  2. A comma-separated list of multipliers (e.g., 2,5)

Using vector arithmetic and recycling:

  1. Create a numeric vector from the base values
  2. Create a numeric vector from the multipliers
  3. Multiply the base values by the multipliers (R will recycle the shorter vector)
  4. Print the result of the multiplication
  5. Add 100 to each element of the original base values (single value recycling)
  6. Print the result of the addition
  7. Subtract the multipliers vector from the base values vector
  8. Print the result of the subtraction

For example, if the inputs are:

5,10,15,20,25,30
3,7

The output should be:

[1]  15  70  45 140  75 210
[1] 105 110 115 120 125 130
[1]  2  3 12 13 22 23

Explanation:

  • Multiplication: The multipliers c(3, 7) are recycled to c(3, 7, 3, 7, 3, 7), resulting in 5*3, 10*7, 15*3, 20*7, 25*3, 30*7
  • Addition: The single value 100 is recycled to add to each element
  • Subtraction: The multipliers are recycled again, resulting in 5-3, 10-7, 15-3, 20-7, 25-3, 30-7

Cheat sheet

When performing arithmetic operations between vectors of different lengths, R uses recycling - the shorter vector is repeated to match the length of the longer one:

numbers <- c(1, 2, 3, 4, 5, 6)
multiplier <- c(10, 100)
print(numbers * multiplier)

Output:

[1]  10 200  30 400  50 600

The multiplier vector is recycled to c(10, 100, 10, 100, 10, 100) before multiplication.

Single value recycling is particularly useful:

prices <- c(10, 20, 30)
print(prices * 1.1)

Output:

[1] 11 22 33

The value 1.1 is recycled to match the length of prices.

Note: R may produce a warning when the longer vector's length isn't a multiple of the shorter one.

Try it yourself

# Read input
con <- file("stdin", "r")
base_input <- suppressWarnings(readLines(con, n = 1))
multipliers_input <- suppressWarnings(readLines(con, n = 1))
close(con)

# Parse the comma-separated values into numeric vectors
base_values <- as.numeric(strsplit(base_input, ",")[[1]])
multipliers <- as.numeric(strsplit(multipliers_input, ",")[[1]])

# TODO: Write your code below
# 1. Multiply base_values by multipliers (recycling will happen automatically)
# 2. Print the multiplication result
# 3. Add 100 to each element of base_values
# 4. Print the addition result
# 5. Subtract multipliers from base_values
# 6. Print the subtraction result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals