Vectors in R
- Vector is a basic data structure in R. It contains element of the same type. The data types can be logical, integer, double, character, complex or raw.
- A vector’s type can be checked with the typeof() function.
- Another important property of a vector is its length. This is the number of elements in the vector and can be checked with the function length().
- Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.
How to Create Vector in R?
- Vectors are generally created using the c() function.
In [41]:
# Atomic vector of type character.
print("Itronix Solutions");
# Atomic vector of type double.
print(94.3)
# Atomic vector of type integer.
print(69L)
# Atomic vector of type logical.
print(FALSE)
# Atomic vector of type complex.
print(2+3i)
# Atomic vector of type raw.
print(charToRaw('itronix'))
[1] "Itronix Solutions" [1] 94.3 [1] 69 [1] FALSE [1] 2+3i [1] 69 74 72 6f 6e 69 78
In [3]:
# Creating a sequence from 5 to 13.
v <- 5:13
print(v)
# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)
# If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)
[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
In [4]:
# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))
[1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
In [2]:
# The logical and numeric values are converted to characters.
s =c('Itronix','Solutions',5,TRUE)
print(s)
[1] "Itronix" "Solutions" "5" "TRUE"
In [4]:
length(s)
typeof(s)
4
‘character’
In [38]:
t = c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
print(t)
print(t[1])
print(t[2:4])
print(t[-1])
print(t[c(2,3,6)])
print(t[c(2:4)])
print(t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)])
print(t[c(0,0,0,0,0,0,1)])
[1] "Sun" "Mon" "Tue" "Wed" "Thurs" "Fri" "Sat" [1] "Sun" [1] "Mon" "Tue" "Wed" [1] "Mon" "Tue" "Wed" "Thurs" "Fri" "Sat" [1] "Mon" "Tue" "Fri" [1] "Mon" "Tue" "Wed" [1] "Sun" "Fri" [1] "Sun"
In [39]:
# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
# Vector addition.
add.result <- v1+v2
print(add.result)
# Vector subtraction.
sub.result <- v1-v2
print(sub.result)
# Vector multiplication.
multi.result <- v1*v2
print(multi.result)
# Vector division.
divi.result <- v1/v2
print(divi.result)
[1] 7 19 4 13 1 13 [1] -1 -3 4 -3 -1 9 [1] 12 88 0 40 0 22 [1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000
In [40]:
v = c(3,8,4,5,0,11, -9, 304)
# 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("Red","Blue","yellow","violet")
sort.result = sort(v)
print(sort.result)
# Sorting character vectors in reverse order.
revsort.result = sort(v, decreasing = TRUE)
print(revsort.result)
[1] -9 0 3 4 5 8 11 304 [1] 304 11 8 5 4 3 0 -9 [1] "Blue" "Red" "violet" "yellow" [1] "yellow" "violet" "Red" "Blue"
In [ ]: