Vectors | Dataplexa

Vectors in R

In this lesson, you will learn about vectors in R. Vectors are one of the most important data structures in R and are used everywhere in data analysis.

Almost all data in R is stored or handled in vector form, so understanding vectors is essential.


What Is a Vector?

A vector is a collection of values of the same data type. These values are stored in a single variable and arranged in a sequence.

For example, a list of numbers, a list of text values, or a list of logical values can all be vectors.


Creating Vectors Using c()

In R, vectors are created using the c() function. The letter c stands for “combine”.

You can combine multiple values into a single vector using this function.

numbers <- c(10, 20, 30, 40)
numbers

All values inside a vector must belong to the same data type.


Types of Vectors

Vectors can store different kinds of data, but each vector can only store one type at a time.

The most common vector types are numeric, character, and logical.


Numeric Vectors

Numeric vectors store numbers. They are commonly used for calculations and statistics.

scores <- c(85, 90, 78, 88)
scores

Character Vectors

Character vectors store text values. All values must be written inside quotation marks.

languages <- c("R", "Python", "SQL")
languages

Logical Vectors

Logical vectors store TRUE or FALSE values. They are useful for conditions and filtering data.

status <- c(TRUE, FALSE, TRUE)
status

Mixing Data Types in a Vector

When you mix different data types in a vector, R converts all values to the same type. This is called type coercion.

R always converts values to the most flexible type, usually character.

mixed <- c(10, "R", TRUE)
mixed

In this case, all values become character values.


Accessing Vector Elements

Each value in a vector has a position number, starting from 1. You can access elements using square brackets.

This is called indexing.

numbers <- c(5, 10, 15, 20)

numbers[1]
numbers[3]

R uses 1-based indexing, unlike many other programming languages.


Accessing Multiple Elements

You can access multiple elements by passing a vector of indexes. This allows flexible data selection.

numbers[c(2, 4)]

This returns the second and fourth elements from the vector.


Vector Length

The length() function returns the number of elements in a vector.

This is useful when looping or validating data.

length(numbers)

Vector Arithmetic

R allows arithmetic operations directly on vectors. Operations are applied element by element.

This feature makes R powerful for data analysis.

a <- c(1, 2, 3)
b <- c(4, 5, 6)

a + b
a * b

Each position is calculated independently.


Creating Sequences

R provides easy ways to create sequences of numbers. The : operator is commonly used.

sequence <- 1:10
sequence

This creates a vector from 1 to 10.


Why Vectors Are Important

Vectors form the foundation of data structures like matrices and data frames.

Most R functions are designed to work directly with vectors.


📝 Practice Exercises


Exercise 1

Create a numeric vector containing five numbers and print it.

Exercise 2

Create a character vector storing three programming languages.

Exercise 3

Access the second element of a vector.

Exercise 4

Find the length of a vector.


✅ Practice Answers


Answer 1

values <- c(10, 20, 30, 40, 50)
values

Answer 2

langs <- c("R", "Python", "SQL")
langs

Answer 3

values[2]

Answer 4

length(values)

What’s Next?

Now that you understand vectors, you are ready to learn about lists in R.

In the next lesson, you will explore how lists store different types of data together. This is an important step toward complex data structures.