R Vectors

Vector is a sequence of data elements of the same type. It is a one dimensional array. Members of a vector are known as components.

There are five classes of vectors:

  • Numeric
  • Integer
  • Complex
  • Logical
  • Character

Creating a Vector

Single element vector

In R, when you write just one value, it is considered as a vector of length 1. Let’s see the different type of vectors with a single element:

# character.
print("Hello")
# double.
print(24.6)
# integer.
print(24L)
# logical.
print(TRUE)
# complex.
print(1+5i)

Output:

[1] "Hello"
[1] 24.6
[1] 24
[1] TRUE
[1] 1+5i

Multiple elements vector

We can create a vector of consecutive numbers through the:

1. colon operator (:)

Example:

> x <- 1:7
> x
[1] 1 2 3 4 5 6 7
2. Using sequence operator (seq.)

Example:

# specify step size
seq(1, 8, by= 2)
#specify length of the vector
seq(1, 5, length.out=9)

Output:

[1] 1 3 5 7
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
3. Using the c() function

Example:

# logical and numeric values are converted to characters.
s <- c('hello','red', 10, TRUE)
print(s)

Output:

[1] "hello" "red"   "10"    "TRUE"

Accessing vector elements

We can access the elements of a vector with the help of indexing. For indexing, we have to use the [] brackets. Each indexing starts with position one. 0, 1 TRUE and FALSE can also be used for indexing. The indexing vector can be logical, integer or character.

Example:

# Accessing vector elements with the help of position.
w <- c("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
u <- w[c(2,4,6)]
print(u)
# Accessing vector elements with the help of logical indexing.
v <- w[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)
# Accessing vector elements with the help of negative indexing.
x <- w[c(-2,-5)]
print(x)
# Accessing vector elements with the help of 0/1 indexing.
y <- w[c(0,0,0,0,0,0,1)]
print(y)

Output:

[1] "Mon" "Wed" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"

Vector Operations

1. Vector Arithmetic

We can perform addition, subtraction, multiplication, and division operation with the two vectors of the same length. the result will be a vector.

Example:

# Create two vectors.
v1 <- c(12,1,0,5,4,2)
v2 <- c(10,4,3,1,2,0)
# addition.
add.result <- v1+v2
print(add.result)
# subtraction.
sub.result <- v1-v2
print(sub.result)
# multiplication.
multi.result <- v1*v2
print(multi.result)
# division.
divi.result <- v1/v2
print(divi.result)

Output:

[1] 22  5  3  6  6  2
[1]  2 -3 -3  4  2  2
[1] 120   4   0   5   8   0
[1] 1.20 0.25 0.00 5.00 2.00  Inf
2. Combining Vectors

Example:

n = c (10, 13, 24)
s = c('Nikita', 'Nidhi', 'Deep')
c(n, s)

Output:

[1] "10"     "13"     "24"     "Nikita" "Nidhi"  "Deep"
3. Sorting a Vector

We can sort the elements of a vector through the sort() function.

v <- c(13, 80, 14, 50, 0, 11, -19, 100)
# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)
# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
# Sorting character vectors.
v <- c("Nikita","Deep","Ridhi","Aman")
sort.result <- sort(v)
print(sort.result)
# Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

Output:

[1] -19   0  11  13  14  50  80 100
[1] 100  80  50  14  13  11   0 -19
[1] "Aman"   "Deep"   "Nikita" "Ridhi"
[1] "Ridhi"  "Nikita" "Deep"   "Aman"
4. Deleting a Vector

A vector can be deleted by simply assigning a NULL to it.

# creating a vector
v <- c(13, 80, 14, 50, 0, 11, -19, 100)
v
# deleting vector v
v <- NULL
v

Output:

[1]  13  80  14  50   0  11 -19 100
NULL
Reference: https://www.datamentor.io/r-programming/vector/ https://data-flair.training/blogs/r-vector/



ADVERTISEMENT
ADVERTISEMENT