Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
A vector in R is an ordered collection of values stored together as one object. The R Language Definition describes vectors as “contiguous cells containing data” and names six basic (“atomic”) vector types: logical, integer, real, complex, character and raw. Even a single number is a vector: “A number occurring by itself in an expression is taken as a vector of length one” (An Introduction to R, section 2.1). This guide goes past the definition to the two rules that cause most confusion in practice, recycling and indexing, and sets them out side by side from the official manuals (checked 2026-10-29).
What a vector is used for in research
Almost every analysis in R starts from vectors. A column of measurements, a set of participant identifiers, a series of p-values and a yes/no filter for including samples are all vectors. A data frame is built from them, so understanding vectors explains how columns behave; see the CASRAI guide to data frames in R. The atomic types and what happens when they are mixed are covered in R programming data types, so this page does not repeat them.
Install or access
R is described on the R Project home page as “a free software environment for statistical computing and graphics.” The home page directs downloads to a CRAN mirror (r-project.org, checked 2026-10-29). The manuals quoted here are published on CRAN as An Introduction to R (the copy checked was for R version 4.6.1, dated 2026-06-24) and the R Language Definition. Add-on packages are a separate step; see how to install R packages.
Step-by-step: create, inspect and index a vector
1. Create a vector with c()
Use c(), which “can take an arbitrary number of vector arguments and whose value is a vector got by concatenating its arguments end to end.” The manual’s example is:
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
The same assignment can be written assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7)) or c(10.4, 5.6, 3.1, 6.4, 21.7) -> x. Because c() concatenates vectors, y <- c(x, 0, x) gives a vector of 11 entries: two copies of x with a zero in the middle.
2. Generate regular sequences: 1:30, seq() and rep()
- The colon operator builds integer runs, and “the colon operator has high priority within an expression.”
1:30is the same asseq(1,30). seq()takes named argumentsfrom,to,byandlength, soseq(-5, 5, by=.2)andseq(length=51, from=-5, by=.2)generate the same vector. If neitherbynorlengthis given, the defaultby=1is assumed.rep(x, times=5)puts five copies ofxend to end, whilerep(x, each=5)repeats each element five times before moving to the next.
3. Check the length
length(x) returns the number of elements in x. Vectors of length zero are allowed, and the language definition calls them “possible (and useful).”
4. Create a logical vector from a condition
“Logical vectors are generated by conditions,” and their elements can be TRUE, FALSE or NA. A condition such as x > 5 therefore yields a logical vector the same length as x, which can be used as an index in step 6.
5. Do arithmetic: the recycling rule
Arithmetic is performed element by element. Vectors in the same expression “need not all be of the same length.” The result has the length of the longest vector, and “the shorter vector is recycled as often as need be (perhaps fractionally) until it matches the length of the longer vector.” A constant is simply repeated. In the manual’s example, with x of length 5 and y of length 11:
v <- 2*x + y + 1
produces a vector of length 11 made by adding “2*x repeated 2.2 times, y repeated just once, and 1 repeated 11 times.” The fractional 2.2 is the point to remember: when lengths do not divide evenly, the last copy of the shorter vector is cut off partway.
6. Pick out elements with an index vector
Put an index vector in square brackets after the vector’s name. The manual lists four kinds, which work as follows (An Introduction to R, section 2.7):
| Index type | What it does | Manual example |
|---|---|---|
| Logical | Elements where the index is TRUE are kept; FALSE are dropped |
y <- x[!is.na(x)] |
| Positive integers | Selects those positions, in the order given | x[1:10] selects the first 10 elements |
| Negative integers | “Specifies the values to be excluded rather than included” | y <- x[-(1:5)] gives all but the first five elements |
| Character strings | Selects by name; only applies where the object has a names attribute | fruit[c("apple","orange")] after names(fruit) <- c("orange", "banana", "apple", "peach") |
The manual notes that “alphanumeric names are often easier to remember than numeric indices,” and that the character form is particularly useful with data frames. An indexed expression can also receive an assignment, which changes only the selected elements. x[is.na(x)] <- 0 replaces every missing value in x with zero.
Output and how to read it
When an expression is used as a complete command, its value is printed. In the manual’s printed output of a longer vector, a line that begins with a number in square brackets shows the position of the first element on that line; for example, the manual’s factor output continues on a second line that starts with [16]. Printing does not change the stored vector: the manual points out that 1/x prints the reciprocals while “the value of x, of course, unchanged.”
Troubleshooting
Why do I get NA when I index past the end?
The R Language Definition (section 3.4.1) states: “If i is positive and exceeds length(x) then the corresponding selection is NA.” Check length(x) before indexing by position.
Why did a negative index drop elements instead of selecting them?
By design. “If i contains negative elements, all elements except those indicated are selected.”
Why does a calculation with different-length vectors still run?
Because of recycling (step 5). R extends the shorter vector rather than stopping, so check length() on both operands whenever the result is not the length you expected.
How do I find missing values?
is.na(x) returns a logical vector of the same size as x, and its result can be used directly as a logical index, as in x[!is.na(x)]. For choosing columns and rows from a data frame once the vectors are built, see select in R.
Free alternatives
Other free scientific tools have a comparable one-dimensional structure. The wording below is taken from each project’s own documentation (checked 2026-10-29).
| Tool | Equivalent structure | What its documentation says |
|---|---|---|
| Julia | Vector |
The Julia manual states that the array literal [A, B, C, ...] “creates a one-dimensional array (i.e., a vector)”; the Julia documentation lists the language as “Free and open source (MIT licensed)” |
| NumPy (Python) | ndarray |
The NumPy documentation says the ndarray object “encapsulates n-dimensional arrays of homogeneous data types” |
Frequently asked questions
What is a vector in R?
An ordered collection of values held in one object. The R Language Definition says “vectors can be thought of as contiguous cells containing data” and lists six atomic types: logical, integer, real, complex, character and raw.
Is there such a thing as a single value in R?
Not as a separate type. A number on its own is “taken as a vector of length one.”
What is the difference between an atomic vector and a list?
Lists, which the language definition calls “generic vectors,” are “another kind of data storage” alongside the six atomic types. Types and conversions are covered in R programming data types, and a data frame stores its columns as vectors (see data frames in R).
What does recycling mean in R?
When vectors of different lengths appear in one arithmetic operation, the shorter one is reused as often as needed, “perhaps fractionally,” until it matches the longer one.
How do I remove elements from a vector?
Index with negative integers, for example x[-(1:5)], which gives all elements except the first five. This returns a new vector; assign it back to x if you want to keep it.
Sources
- An Introduction to R, R Core Team, CRAN, sections 2.1 to 2.7 (R 4.6.1 copy, checked 2026-10-29)
- R Language Definition, R Core Team, CRAN, sections 2.1 and 3.4.1 (checked 2026-10-29)
- The R Project for Statistical Computing (checked 2026-10-29)
- Julia manual, Single- and multi-dimensional Arrays and Julia documentation home (checked 2026-10-29)
- What is NumPy?, NumPy documentation (checked 2026-10-29)







