Vectors in R Programming

An R vector is a one-dimensional, ordered collection of values that share a common atomic type. Vectors are used throughout R to store numbers, text, logical values, complex numbers, and raw bytes.

A vector in R can be compared to a one-dimensional array in programming languages such as C and Java. However, R is designed around vector operations, so many calculations can be performed on an entire vector without writing an explicit loop.

Atomic vectors can contain values of one of these types: logical, integer, double, complex, character, or raw. The length of a vector may be changed by adding or removing elements, so a vector should not be treated as permanently fixed in size.

Create a Vector in R with the c() Function

The most common way to make a vector in R is to use the c() function. The letter c means combine or concatenate.

</>
Copy
numbers <- c(10, 20, 30, 40)
fruits <- c("apple", "banana", "mango")
flags <- c(TRUE, FALSE, TRUE)

Enter the vector name to display its values.

</>
Copy
numbers
[1] 10 20 30 40

Atomic Vector Types in R

Every atomic vector has one underlying data type. The following examples show the commonly used vector types.

</>
Copy
logical_vector <- c(TRUE, FALSE, TRUE)
integer_vector <- c(1L, 2L, 3L)
double_vector <- c(1.5, 2.75, 3.25)
complex_vector <- c(2 + 3i, 4 - 1i)
character_vector <- c("red", "green", "blue")
raw_vector <- charToRaw("R")

Use typeof() to inspect the internal type of a vector and class() to inspect its class.

</>
Copy
typeof(integer_vector)
typeof(double_vector)
class(character_vector)
[1] "integer"
[1] "double"
[1] "character"

Generate Numeric Vectors with Sequences and Repetition

R provides several functions for creating regularly spaced or repeated values.

  • : creates an integer sequence.
  • seq() creates a sequence with a chosen step size or length.
  • rep() repeats values.
</>
Copy
one_to_five <- 1:5
by_twos <- seq(from = 2, to = 10, by = 2)
repeated_values <- rep(c("A", "B"), times = 3)

one_to_five
by_twos
repeated_values
[1] 1 2 3 4 5
[1]  2  4  6  8 10
[1] "A" "B" "A" "B" "A" "B"

How R Handles Mixed Values in a Vector

An atomic vector cannot store unrelated types independently. When values of different types are combined, R converts them to a common type. This process is called type coercion.

</>
Copy
mixed_numeric <- c(TRUE, 5L, 2.5)
mixed_character <- c(10, FALSE, "R")

typeof(mixed_numeric)
mixed_numeric

typeof(mixed_character)
mixed_character
[1] "double"
[1] 1.0 5.0 2.5
[1] "character"
[1] "10"    "FALSE" "R"

In general, coercion moves from logical to integer, double, complex, and then character when a common type is required. Use a list instead of an atomic vector when each element must retain a different type.

Access Vector Elements by Position

R uses one-based indexing. The first element is at position 1, not position 0.

</>
Copy
scores <- c(72, 85, 91, 68, 88)

scores[1]
scores[c(2, 4)]
scores[2:4]
[1] 72
[1] 85 68
[1] 85 91 68

A negative index excludes a position from the result.

</>
Copy
scores[-3]
[1] 72 85 68 88

Filter an R Vector with Logical Conditions

A logical expression produces a vector of TRUE and FALSE values. That logical vector can be used inside square brackets to select matching elements.

</>
Copy
scores <- c(72, 85, 91, 68, 88)

scores >= 80
scores[scores >= 80]
[1] FALSE  TRUE  TRUE FALSE  TRUE
[1] 85 91 88

Use which() when you need the positions of matching elements rather than their values.

</>
Copy
which(scores >= 80)
[1] 2 3 5

Name Vector Elements in R

A named vector associates a label with each value. Names can be assigned while creating the vector or later with the names() function.

</>
Copy
prices <- c(apple = 45, banana = 20, mango = 60)

prices["banana"]
names(prices)
banana 
    20
[1] "apple"  "banana" "mango"

You can also assign names after creating the values.

</>
Copy
temperatures <- c(30, 32, 29)
names(temperatures) <- c("Monday", "Tuesday", "Wednesday")

temperatures
   Monday   Tuesday Wednesday 
       30        32        29

Modify, Add, and Remove Vector Elements

Assign a new value to an indexed position to modify an element. Use c() to append values, or negative indexing to create a vector without selected positions.

</>
Copy
values <- c(10, 20, 30)

values[2] <- 25
values <- c(values, 40)
values <- values[-1]

values
[1] 25 30 40

Perform Vectorized Arithmetic in R

Arithmetic operators work element by element when applied to vectors of compatible lengths.

</>
Copy
x <- c(2, 4, 6)
y <- c(1, 3, 5)

x + y
x * y
x ^ 2
[1]  3  7 11
[1]  2 12 30
[1]  4 16 36

This vectorized approach is usually clearer than processing each element with a manual loop.

Understand Vector Recycling in R

When vectors have different lengths, R may repeat the shorter vector until it matches the length of the longer vector. This behavior is called vector recycling.

</>
Copy
c(10, 20, 30, 40) + c(1, 2)
[1] 11 22 31 42

Here, c(1, 2) is reused as c(1, 2, 1, 2). R produces a warning when the longer vector’s length is not an exact multiple of the shorter vector’s length.

</>
Copy
c(10, 20, 30, 40, 50) + c(1, 2)
Warning message:
In c(10, 20, 30, 40, 50) + c(1, 2) :
  longer object length is not a multiple of shorter object length
[1] 11 22 31 42 51

Find Vector Length, Missing Values, and Summary Statistics

R includes built-in functions for inspecting and summarizing vectors.

</>
Copy
measurements <- c(12, 18, NA, 25, 20)

length(measurements)
is.na(measurements)
sum(measurements, na.rm = TRUE)
mean(measurements, na.rm = TRUE)
min(measurements, na.rm = TRUE)
max(measurements, na.rm = TRUE)
[1] 5
[1] FALSE FALSE  TRUE FALSE FALSE
[1] 75
[1] 18.75
[1] 12
[1] 25

The argument na.rm = TRUE tells a function to remove missing values before calculating the result.

Sort and Reverse an R Vector

Use sort() to arrange values and rev() to reverse their current order.

</>
Copy
values <- c(8, 3, 10, 1, 6)

sort(values)
sort(values, decreasing = TRUE)
rev(values)
[1]  1  3  6  8 10
[1] 10  8  6  3  1
[1]  6  1 10  3  8

Convert an R Vector to Another Type

Use conversion functions such as as.integer(), as.double(), as.logical(), and as.character() when a vector must be converted explicitly.

</>
Copy
text_numbers <- c("10", "20", "30")
numeric_values <- as.integer(text_numbers)

numeric_values
typeof(numeric_values)
[1] 10 20 30
[1] "integer"

Conversion can produce NA and a warning when a value cannot be represented in the requested type. For example, as.integer("apple") cannot produce a valid integer.

Difference Between an R Vector and an R List

An atomic vector stores values of one common type. A list can hold elements of different types and structures without coercing them to one atomic type.

</>
Copy
atomic_vector <- c(1, TRUE, "R")
mixed_list <- list(1, TRUE, "R", c(10, 20))

atomic_vector
mixed_list

The values in atomic_vector are converted to character values. The list retains the number, logical value, character value, and nested numeric vector as separate types.

R Vector Tutorials by Operation

Create Logical, Integer, Double, and Character Vectors

Access, Measure, Iterate, and Delete Vector Elements

Sort Values in an R Vector

Apply Arithmetic, Recycling, Type Inspection, and Reversal

Check Vector Values and Atomic Types

Convert Logical and Character Vectors

Common R Vector Mistakes

  • Using index zero: R starts vector indexing at 1. An index of 0 selects no elements.
  • Mixing types unintentionally: Adding a character value to a numeric vector can coerce every element to character.
  • Ignoring recycling warnings: Arithmetic on unequal vector lengths may produce unintended results.
  • Comparing missing values with ==: Use is.na(x) instead of x == NA.
  • Confusing a vector with a list: Use a list when elements must retain different data types.

Frequently Asked Questions About R Vectors

What is an R vector?

An R vector is an ordered, one-dimensional data structure whose elements share a common atomic type. Examples include numeric, logical, and character vectors.

How do I make a vector in R?

Use the c() function to combine values, such as c(10, 20, 30). You can also use 1:5, seq(), or rep() to generate patterned vectors.

Can an R vector contain different data types?

An atomic vector has one common type. When different types are combined, R coerces them to a compatible type. Use an R list when elements must retain different types.

How do I name elements in an R vector?

Create the vector with labels, such as c(red = 10, blue = 20), or assign labels later with names(vector) <- c("red", "blue").

What is vector recycling in R?

Vector recycling occurs when R repeats a shorter vector during an operation with a longer vector. A warning is generated when the longer length is not an exact multiple of the shorter length.

Editorial QA Checklist for This R Vector Tutorial

  • Verify that every indexing example uses R’s one-based indexing rules.
  • Confirm that atomic vector examples preserve a single underlying type or clearly explain coercion.
  • Run each R code sample and compare the displayed output with the current R result.
  • Check that examples involving NA use is.na() or na.rm = TRUE correctly.
  • Review recycling examples to ensure warnings are explained when vector lengths are incompatible.

Summary of R Vector Operations

In this R Tutorial, we learned how to create vectors, inspect their types, access and filter elements, assign names, modify values, perform vectorized arithmetic, handle recycling, work with missing values, sort data, and convert vectors between types. These operations form the basis of many data-processing tasks in R.