A vector is R’s most basic data structure: an ordered set of values of the same type. Even a single number is a vector of length 1. Almost everything in R works on vectors.
What You Will Learn
- Create vectors with :, seq() and c()
- Type coercion
- Indexing with positions, negatives and logicals
- Vector arithmetic and recycling
- Sorting
Prerequisites
Creating Vectors
print(5:13)
print(6.6:12.6)
print(3.8:11.4)
print(seq(5, 9, by = 0.4))
[1] 5 6 7 8 9 10 11 12 13 [1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6 [1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8 [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
Explanation: : steps by 1 and stops before passing the end (11.4 is not reached). seq() lets you choose the step.
c() and Coercion
s <- c("Itronix", "Solutions", 5, TRUE)
print(s)
length(s); typeof(s)
[1] "Itronix" "Solutions" "5" "TRUE" 4 'character'
Explanation: All elements must share a type, so numbers and logicals are converted (coerced) to text when any element is text. Order of coercion: logical → integer → double → character.
Indexing
t <- c("Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat")
print(t[1])
print(t[2:4])
print(t[-1])
print(t[c(2, 3, 6)])
print(t[c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)])
[1] "Sun" [1] "Mon" "Tue" "Wed" [1] "Mon" "Tue" "Wed" "Thurs" "Fri" "Sat" [1] "Mon" "Tue" "Fri" [1] "Sun" "Fri"
Explanation: R indexes from 1, not 0. A negative index removes that element. A logical vector keeps the TRUE positions – the basis of filtering like marks[marks > 50].
Arithmetic
v1 <- c(3, 8, 4, 5, 0, 11)
v2 <- c(4, 11, 0, 8, 1, 2)
print(v1 + v2)
print(v1 * v2)
print(v1 / v2)
print(c(1, 2, 3, 4) * c(10, 100))
[1] 7 19 4 13 1 13 [1] 12 88 0 40 0 22 [1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000 [1] 10 200 30 400
Explanation: Operations are element-wise. Dividing by 0 gives Inf. When lengths differ, the shorter vector is recycled (10, 100, 10, 100).
Sorting
v <- c(3, 8, 4, 5, 0, 11, -9, 304)
print(sort(v))
print(sort(v, decreasing = TRUE))
print(sort(c("Red", "Blue", "yellow", "violet")))
[1] -9 0 3 4 5 8 11 304 [1] 304 11 8 5 4 3 0 -9 [1] "Blue" "Red" "violet" "yellow"
Useful Vector Functions
| Function | Example | Result |
|---|---|---|
| length() | length(1:10) | 10 |
| sum(), mean() | mean(c(2, 4, 6)) | 4 |
| max(), min() | max(c(3, 9, 1)) | 9 |
| rev() | rev(1:3) | 3 2 1 |
| which() | which(c(5, 12, 8) > 6) | 2 3 |
| unique() | unique(c(1, 1, 2)) | 1 2 |
Series
R series: Install R · Introduction to R · R in Jupyter · Practical Session · Operators · Decision Making · Loops · Functions · Strings · cat() and paste() · Vectors · Lists · Matrices · Data Frames
Interview Questions
Q: Does R index from 0 or 1?
From 1.
Q: What is recycling?
Repeating the shorter vector to match the longer one in element-wise operations.
Practice Exercises
- Create a vector of 10 marks and print those above the mean.
- Reverse a vector without rev().
Summary
Vectors hold same-type values, are indexed from 1, support logical filtering, and do element-wise arithmetic with recycling.
Next: Lists
Machine Learning Tutorials, Courses and Certifications