R Multiple Variables

R lets you create several variables at once, chain one value across several names, or bundle many related values into a single vector.

#Jobseeker

Find matching jobs & Auto apply with AI

Hyring
Jobseeker using Hyring to search, apply, and prepare with AI

Assigning Several Variables in One Go

You can assign values to multiple variables one after another on separate lines, which is the most common and clearest approach in R scripts.

Example

first_name <- "Maya"
last_name <- "Chen"
age <- 34
print(first_name)
print(last_name)
print(age)

Chained Assignment

R also allows chained assignment, where the same value is assigned to multiple variables in a single statement by stringing assignment arrows together. R evaluates this right to left, so the rightmost value is assigned to every variable in the chain.

Example

x <- y <- z <- 10
print(x)
print(y)
print(z)

Combining Values with c()

To store several related values under one variable name, combine them into a vector with the c() function (short for "combine"). This is different from creating separate variables — it creates a single vector containing multiple elements, accessed by position.

Example

scores <- c(88, 92, 79, 95)
print(scores)
print(scores[2])
Note: Vector indexing in R starts at 1, not 0, so scores[1] is the first element and scores[2] is the second — there is no scores[0].

Base R Has No Built-In Unpacking

Some languages let you unpack several values from one line, like a, b <- 1, 2. Base R does not include that syntax. The zeallot package adds a %<-% operator that mimics it, but writing separate assignment lines, or building a named list, remains the standard base-R approach.