HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
R›Vectors

Vectors

Creating vectors with c(), 1-based indexing, length (length) and iterating with a loop.

Simple enough for a 5-year-old to understand

Create and access vector

The fundamental data structure in R is the vector. It is created with c(); it is 1-indexed.

Example code
1
languages <- c("R", "Python", "Julia")
Vector with three elements.
VariableVector with three elements.
2
cat(languages[1], "\n", sep="")
R (FIRST element, index 1!)
Output / InputR (FIRST element, index 1!)
3
cat(length(languages), "\n", sep="")
3 (number of elements)
Output / Input3 (number of elements)
VariableOutput / Input
💡 Hint: R vectors are 1-indexed: the first element is v[1].

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How do you get the FIRST element in the vector c("al", "mor")?

About this topic

Vector is the fundamental data structure of R. It holds values of the same type in an ordered manner and is created with the c function. Almost everything in R is actually a vector; even a single number is a vector of length one.

The most powerful aspect of vectors is that operations are applied to all elements simultaneously. When you multiply a vector by two, all elements are multiplied by two; you don't need to write a loop. This both shortens the code and runs noticeably fast in R.

Indexing is one of the most distinctive features of R. Indices start at one, a negative index means excluding that element, and you can filter using a logical vector. These three features together create a very powerful structure that takes some getting used to.

What you will learn

  • Creating vectors
  • Indexing methods
  • The meaning of negative index
  • Logical filtering
  • Vectorized operations

Who is this for?

  • Learners of R's data structures
  • Developers accustomed to zero-based indexing
  • Analysts who will perform data filtering

When to use?

A vector is used to hold all observations of a single measurement; each column of a data frame is actually a vector.

To select observations matching a condition, logical filtering is both shorter and faster than writing a loop.

Common mistakes

Wrong

v <- c(10, 20, 30)
print(v[0])

In R, indices start at one and there is no such thing as a zeroth element; this expression does not throw an error but returns an empty vector. Returning empty silently makes it hard to notice the error, because subsequent calculations will also silently produce empty results.

Correct

v <- c(10, 20, 30)
print(v[1])

Wrong

v <- c(10, 20, 30)
print(v[-1])

There is no error here, but the expectation might be wrong: a negative index, which means the last element in Python, means "exclude this element" in R. The result will be 20 and 30. To get the last element, you need to use the length.

Correct

v <- c(10, 20, 30)
print(v[length(v)])

Wrong

v <- c(10, 20, 30)
print(v[10])

An out-of-bounds index does not throw an error in R, it returns NA. This behavior hides the error: the calculation continues and the result silently turns into a missing value. Accessing elements with an external index without checking the array's length is risky.

Correct

v <- c(10, 20, 30)
i <- 10
if (i <= length(v)) print(v[i]) else print("out of range")

Frequently asked questions

How do I add elements to a vector?

You combine the existing vector and the new element with the c function. However, this generates a new vector every time; doing this repeatedly inside a loop is very slow for large data. Pre-allocating the size is the correct approach.

How does logical filtering work?

You provide a vector of true-false values of the same length as the vector; only the elements at the positions that are true are returned. Since a conditional expression already produces such a vector, filtering can be written in a single line.

Can vector elements be named?

Yes, you can assign names to each element using the names function and then access the elements by their names. It significantly increases readability in small tables and lookup-purpose mappings.

Summary

Vector is the fundamental data structure of R.

Indices start at one.

Negative index excludes the element.

Out-of-bounds index returns NA, does not throw an error.

Filtering can be done with a logical vector.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code