Vectors in R
Vectors in R
Introduction
A vector is the simplest and most fundamental data structure in R. It is a one-dimensional collection of elements of the same data type arranged sequentially. Vectors are used to store multiple values in a single variable and are widely used for data manipulation and statistical computations.
Examples:
- Marks of students
- Temperatures recorded over a week
- Names of employees
- Boolean values indicating pass/fail
Characteristics of Vectors
- A vector contains elements of the same data type.
- Elements are stored in contiguous locations.
- Elements are indexed starting from 1.
- Vectors are homogeneous.
- Vectors can be numeric, character, logical, complex, or integer.
- Vector operations are performed element-wise.
Creating Vectors
Vectors are commonly created using the c() (combine) function.
Syntax
c(value1, value2, value3, ...)
Example 1: Numeric Vector
marks <- c(85, 90, 75, 88, 92)
print(marks)
Output
[1] 85 90 75 88 92
Example 2: Character Vector
names <- c("John", "Mary", "Alex")
print(names)
Output
[1] "John" "Mary" "Alex"
Example 3: Logical Vector
status <- c(TRUE, FALSE, TRUE)
print(status)
Output
[1] TRUE FALSE TRUE
Type Conversion
If elements of different types are combined, R converts them to a common type.
v <- c(10, TRUE, "Hello")
print(v)
Output
[1] "10" "TRUE" "Hello"
All elements become character.
Determining Vector Type
class()
x <- c(10,20,30)
print(class(x))
Output
[1] "numeric"
typeof()
print(typeof(x))
Output
[1] "double"
length()
Returns the number of elements.
print(length(x))
Output
[1] 3
Generating Sequences
Using :
x <- 1:10
print(x)
Output
[1] 1 2 3 4 5 6 7 8 9 10
Using seq()
x <- seq(1,10,2)
print(x)
Output
[1] 1 3 5 7 9
Using rep()
x <- rep(5,4)
print(x)
Output
[1] 5 5 5 5
Accessing Vector Elements
Vector indices begin at 1.
marks <- c(85,90,75,88,92)
print(marks[1])
Output
[1] 85
Access Multiple Elements
print(marks[c(2,4)])
Output
[1] 90 88
Access a Range of Elements
print(marks[2:4])
Output
[1] 90 75 88
Negative Indexing
Removes specified elements.
print(marks[-2])
Output
[1] 85 75 88 92
Modifying Elements
marks <- c(85,90,75)
marks[2] <- 95
print(marks)
Output
[1] 85 95 75
Adding Elements
marks <- c(85,90,75)
marks <- c(marks,88)
print(marks)
Output
[1] 85 90 75 88
Deleting Elements
marks <- c(85,90,75,88)
marks <- marks[-3]
print(marks)
Output
[1] 85 90 88
Vector Arithmetic
Arithmetic operations are performed element-wise.
a <- c(1,2,3)
b <- c(4,5,6)
print(a+b)
Output
[1] 5 7 9
Subtraction
print(b-a)
Output
[1] 3 3 3
Multiplication
print(a*b)
Output
[1] 4 10 18
Division
print(b/a)
Output
[1] 4.0 2.5 2.0
Scalar Operations
a <- c(1,2,3)
print(a+10)
Output
[1] 11 12 13
print(a*5)
Output
[1] 5 10 15
Vector Comparison
a <- c(1,2,3)
b <- c(2,2,4)
print(a>b)
Output
[1] FALSE FALSE FALSE
print(a==b)
Output
[1] FALSE TRUE FALSE
Vectorized Functions
sum()
x <- c(10,20,30)
print(sum(x))
Output
[1] 60
mean()
print(mean(x))
Output
[1] 20
max()
print(max(x))
Output
[1] 30
min()
print(min(x))
Output
[1] 10
sort()
x <- c(50,20,10,40)
print(sort(x))
Output
[1] 10 20 40 50
rev()
print(rev(x))
Output
[1] 40 10 20 50
Logical Indexing
marks <- c(85,40,95,60,30)
print(marks[marks>=50])
Output
[1] 85 95 60
Find Even Numbers
x <- 1:10
print(x[x%%2==0])
Output
[1] 2 4 6 8 10
Named Vectors
marks <- c(John=85, Mary=90, Alex=75)
print(marks)
Output
John Mary Alex
85 90 75
Access by Name
print(marks["Mary"])
Output
Mary
90
Vector Recycling
When vectors of unequal lengths are involved, the shorter vector is repeated.
a <- c(1,2,3,4)
b <- c(10,20)
print(a+b)
Output
[1] 11 22 13 24
because
10 20 10 20
is recycled.
Useful Functions
| Function | Purpose |
|---|---|
c() | Create vector |
length() | Number of elements |
class() | Type of vector |
typeof() | Storage mode |
seq() | Generate sequence |
rep() | Repeat values |
sort() | Sort elements |
rev() | Reverse elements |
sum() | Sum of elements |
mean() | Average |
max() | Maximum element |
min() | Minimum element |
Applications of Vectors
- Store marks of students.
- Store monthly sales data.
- Perform statistical calculations.
- Process signals and time-series data.
- Perform mathematical and logical operations.
Example Program
marks <- c(85,90,75,88,92)
cat("Marks =", marks, "\n")
cat("Average =", mean(marks), "\n")
cat("Maximum =", max(marks), "\n")
cat("Minimum =", min(marks), "\n")
cat("Passed Students =", marks[marks>=50])
Output
Marks = 85 90 75 88 92
Average = 86
Maximum = 92
Minimum = 75
Passed Students = 85 90 75 88 92
Advantages of Vectors
- Simple and efficient.
- Support vectorized operations.
- Reduce the need for loops.
- Provide fast computation.
- Widely used in statistical analysis and machine learning.
Conclusion
Vectors are the basic building blocks of data structures in R. They provide an efficient way to store homogeneous data and support powerful element-wise operations and statistical functions. Understanding vectors is essential for mastering more advanced R data structures such as matrices, arrays, lists, and data frames.
Comments
Post a Comment