In R programming, there are several
common vector operations that you can perform on one or more vectors.
Some of the most common operations
include subsetting, indexing, and slicing, which allow you to extract specific
elements or subsets of elements from a vector.
Other operations include arithmetic
operations, logical operations, and comparison operations, which allow you to
perform mathematical calculations, logical tests, and comparisons between
vectors.
Ø
Examples
of common vector operations in R:
a.
Indexing:
Indexing is used to extract individual elements or subsets of elements from a
vector.
Syntax: vector[index]
where a vector is the vector
you want to index and index is a numeric or logical vector indicating the
positions of the elements you want to extract.
For
example:
x
<- c(1, 2, 3, 4, 5)
x[3]
# returns the third element of x, which is 3
[1]
3
x[c(1,
3, 5)] # returns a new vector containing the first, third, and fifth elements
of x
[1]
1 3 5
b.
Slicing:
Slicing is used to extract contiguous subsets of a vector.
Syntax: vector[start:end]
Where a vector is the vector you want to slice, start is the index of the first element you want to include in the slice, and end is the index of the last element you want to include in the slice.
For example:
x
<- c(1, 2, 3, 4, 5)
x[2:4]
# returns a new vector containing the second, third, and fourth elements of x
[1]
2 3 4
c.
Subsetting: Subsetting allows you to extract
specific elements from a vector based on their position or value.
Syntax:
vector[index]
Where the vector is the name of
the vector, and index is the position or positions of the elements to be
extracted.
For
example:
x
<- c(1, 2, 3, 4, 5)
x[x
> 3] # returns a new vector containing only the elements of x that are
greater than 3
4
5
d.
Sorting:
Sorting is used to arrange the elements of a vector in ascending or descending
order.
Syntax:
sort(vector)
where vector is the vector you want to sort. By default, sort() arranges the elements of vector in ascending order, but you can also specify decreasing = TRUE to sort in descending order.
For example:
x
<- c(3, 1, 4, 1, 5, 9, 2, 6, 5)
sort(x)
# returns a new vector containing the elements of x in ascending order
[1]
1 1 2 3 4 5 5 6 9
sort(x,
decreasing = TRUE) # returns a new vector containing the elements of x in descending
order
[1]
9 6 5 5 4 3 2 1 1
e.
Merging: Merging
is used to combine two or more vectors into a single vector.
Syntax:
c(vector1, vector2,
...)
where
vector1, vector2, etc. are the vectors you want to merge.
For
example:
x
<- c(1, 2, 3)
y
<- c(4, 5, 6)
z
<- c(x, y) # returns a new vector containing the elements of x followed by
the elements of y
[1]
1 2 3 4 5 6
f.
Arithmetic operations:
Arithmetic operations allow you to perform mathematical calculations on one or more vectors.
Syntax:
vector1 OP vector2
where
vector1 and vector2 are the vectors to be operated on, and OP is the arithmetic
operator (+, -, *, /, etc.).
For
example:
x
<- c(10, 20, 30)
y
<- c(5, 10, 15)
z
<- x + y # adds x and y element-wise
[1]
15 30 45
g.
Logical operations:
Logical
operations allow you to perform logical tests on one or more vectors.
Syntax: vector1 LOG_OP vector2
where
vector1 and vector2 are the vectors to be operated on, and LOG_OP is the
logical operator (&, |, !, etc.).
For
example:
x
<- c(TRUE, FALSE, TRUE)
y
<- c(FALSE, TRUE, FALSE)
z
<- x & y # returns a vector of length 3 with values FALSE, FALSE, and
FALSE
[1]
FALSE FALSE FALSE
h.
Comparison operations:
Comparison
operations allow you to compare elements of one or more vectors.
Syntax:
vector1 CMP_OP vector2
where
vector1 and vector2 are the vectors to be compared, and CMP_OP is the
comparison operator (<, >, ==, !=, etc.).
For
example:
x
<- c(10, 20, 30)
y
<- c(20, 30, 40)
z
<- x < y # returns a vector of length 3 with values TRUE, TRUE, and TRUE
[1]
TRUE TRUE TRUE
No comments:
Post a Comment