R Vectors
R vectors are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.
Types of R vectors
Vectors are of different types which are used in R. Following are some of the types of vectors:
Numeric vectors: Numeric vectors are those which contain numeric values such as integer, float, etc.
Example:
# R program to create numeric Vectors
# creation of vectors using c() function.
v1<- c(4, 5, 6, 7)
# display type of vector
typeof(v1)
# by using 'L' we can specify that we want integer values.
v2<- c(1L, 4L, 2L, 5L)
# display type of vector
typeof(v2)
Output:
[1] "double"
[1] "integer"
Character vectors: Character vectors in R contain alphanumeric values and special characters.
Example:
# R program to create Character Vectors
# by default numeric values
# are converted into characters
v1<- c('geeks', '2', 'hello', 57)
# Displaying type of vector
typeof(v1)
Output:
[1] "character"
Logical vectors: Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for Null values.
Example:
# R program to create Logical Vectors
# Creating logical vector
# using c() function
v1<- c(TRUE, FALSE, TRUE, NA)
# Displaying type of vector
typeof(v1)
Output:
[1] "logical"
Vectors containing complex values.
Example:
comp_vec <- c(12+1i,3i,5+4i,4+9i,6i)
print(comp_vec)
Output:
Output:
[1]12+1i 0+3i 5+4i 4+9i 0+6i
Creating a vector
There are different ways of creating R vectors. Generally, we use ‘c’ to combine different elements together.
Example:
# R program to create Vectors
# we can use the c function
# to combine the values as a vector.
# By default the type will be double
X<- c(61, 4, 21, 67, 89, 2)
cat('using c function', X, '\n')
# seq() function for creating
# a sequence of continuous values.
# length.out defines the length of vector.
Y<- seq(1, 10)
cat('using seq() function', Y, '\n')
Y<- seq(1, 10,2)
cat('using seq() function', Y, '\n')
Y<- seq(1, 10, length.out = 5)
cat('using seq() function', Y, '\n')
# use':' to create a vector
# of continuous values.
Z<- 2:7
cat('using colon', Z)
Output:
using c function 61 4 21 67 89 2
using seq() function 1 2 3 4 5 6 7 8 9 10
using seq() function 1 3 5 7 9
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
# Create a numeric vector
x <- c(1, 2, 3, 4, 5)
# Find the length of the vector
length(x)
# Create a character vector
y <- c("apple", "banana", "cherry")
# Find the length of the vector
length(y)
# Create a logical vector
z <- c(TRUE, FALSE, TRUE, TRUE)
# Find the length of the vector
length(z)
Output:
[1] 5
[1] 3
[1] 4
Accessing R vector elements
Accessing elements in a vector is the process of performing operation on an individual element of a vector. There are many ways through which we can access the elements of the vector. The most common is using the ‘[]’, symbol.
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.
Example:
# R program to access elements of a Vector
# accessing elements with an index number.
X<- c(2, 5, 18, 1, 12)
cat('Using Subscript operator', X[2], '\n')
# by passing a range of values
# inside the vector index.
Y<- c(4, 8, 2, 1, 17)
cat('Using combine() function', Y[c(4, 1)], '\n')
Output:
Using Subscript operator 5
Using combine() function 1 4
Modifying a R vector
Modification of a Vector is the process of applying some operation on an individual element of a vector to change its value in the vector. There are different ways through which we can modify a vector:
Example:
# R program to modify elements of a Vector
# Creating a vector
X<- c(2, 7, 9, 7, 8, 2)
# modify a specific element
X[3] <- 1
X[2] <-9
cat('subscript operator', X, '\n')
# Modify using different logics.
X[1:5]<- 0
cat('Logical indexing', X, '\n')
# Modify by specifying
# the position or elements.
X<- X[c(3, 2, 1)]
cat('combine() function', X)
Output:
subscript operator 2 9 1 7 8 2
Logical indexing 0 0 0 0 0 2
combine() function 0 0 0
Deletion of a Vector is the process of deleting all of the elements of the vector. This can be done by assigning it to a NULL value.
Example:
# R program to delete a Vector
# Creating a Vector
M<- c(8, 10, 2, 5)
# set NULL to the vector
M<- NULL
print('Output vector')
print(M)
Output:
[1] "Output vector"
NULL
Sorting elements of a R Vector
sort() function is used with the help of which we can sort the values in ascending or descending order.
# R program to sort elements of a Vector
# Creation of Vector
X<- c(8, 2, 7, 1, 11, 2)
# Sort in ascending order
A<- sort(X)
cat('ascending order', A, '\n')
# sort in descending order
# by setting decreasing as TRUE
B<- sort(X, decreasing = TRUE)
cat('descending order', B)
Output:
ascending order 1 2 2 7 8 11
descending order 11 8 7 2 2 1
Negative Indexing
You can also use negative index numbers to access all items except the ones specified:
Example:
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Access all items except for the first item
fruits[c(-1)]
Output:
[1] "apple" "orange" "mango" "lemon"
Repeat Vectors
To repeat vectors, use the rep() function:
Example:
repeat_each <- rep(c(1,2,3), each = 3)
print(repeat_each)
Output:
[1] 1 1 1 2 2 2 3 3 3
repeat_times <- rep(c(1,2,3), times = 3)
print(repeat_times)
Output:
[1] 1 2 3 1 2 3 1 2 3
repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))
print(repeat_indepent)
Output:
[1] 1 1 1 1 1 2 2 3
Generating Sequenced Vectors
One of the examples on top, showed you how to create a vector with numerical values in a sequence with the : operator:
Example
numbers <- 1:10
print(numbers)
Output:
[1] 1 2 3 4 5 6 7 8 9 10
To make bigger or smaller steps in a sequence, use the seq() function:
Example
numbers <- seq(from = 0, to = 100, by = 20)
print(numbers)
Output:
[1] 0 20 40 60 80 100
Note: The seq() function has three parameters: from is where the sequence starts, to is where the sequence stops, and by is the interval of the sequence.
We can also access all elements of the vector by using a for loop. For example,
In R, we can also loop through each element of the vector using the for loop. For example,
numbers <- c(1, 2, 3, 4, 5)
# iterate through each elements of numbers for (number in numbers) {
print(number)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Vector Arithmetic
R performs arithmetic operations on vectors memberwise. This means that the operations are performed on each member. For example:
Adding a Constant
vec=c(1,2,3)
vec_plus_three <- vec+3 print( vec_plus_three)
Output:
[1]4 5 6
Subtracting a constant:
Subtracting a constant:
vec=c(1,2,3)
vec_min_one <- vec-1
print(vec_min_one)
Multiplication with a constant
Output:
[1]0 1 2
Multiplication with a constant
multivec <- vec*2
print(multivec)
Output:
[1] 2 4 6
We can also perform an arithmetic operation like an addition of two vectors of equal length. This adds the corresponding members in the two vectors. For example
Adding two vectorsvec1=c(1,2,3)
vec2=c(1,2,3)
addv<- vec1+vec2
print(addv)
Output:
[1] 2 4 6
Subtracting two vectors
vec1=c(1,2,3)
vec2=c(1,2,3)
subvv<- vec1-vec2
print(subv)
Output:
[1] 0 0 0
Multiplying two vectors
vec1=c(1,2,3)
vec2=c(1,2,3)
mulv<- vec1*vec2
print(mulv)
Output:
[1] 1 4 9
Division of Vector
vec1=c(1,2,3)
vec2=c(1,2,3)
divv<- vec1/vec2
print(divv)
Output:
[1] 1 1 1
vec1=c(1,2,3,4)
vec2=c(1,2)
sumv<- vec1+vec2
print(sumv)
Output:
[1] 2 4 4 6
Comments
Post a Comment