Vectors

A vector is a sequence of data elements of the same basic type. Members in a vector are officially called components. 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.

Creating Vectors Using c() function:

The c() function can be used to create vectors of objects by concatenating things together. 

x <- c(1,2,3,4,5) #double
x #If you use only x auto-printing occurs
l <- c(TRUE, FALSE) #logical
l <- c(T, F) ## logical
c <- c("a", "b", "c", "d") ## character
i <- 1:20 ## integer
cm <- c(2+2i, 3+3i) ## complex
print(l)
print(c)
print(i)
print(cm)

Output:

print(l)
[1] TRUE FALSE
> print(c)
[1] "a" "b" "c" "d"
> print(i)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> print(cm)
[1] 2+2i 3+3i

You can see the type of each vector using typeof() function in R.

typeof(x)
typeof(l)
typeof(c)
typeof(i)
typeof(cm)

Output:

typeof(x)
[1] "double"
> typeof(l)
[1] "logical"
> typeof(c)
[1] "character"
> typeof(i)
[1] "integer"
> typeof(cm)
[1] "complex"

Creating a vector using : operator:

You can create a vector of consecutive numbers using : operator.

 

x<- 1:10
x
typeof(x)

Output:

x
[1] 1 2 3 4 5 6 7 8 9 10
> typeof(x)
[1] "integer"

Creating a vector using seq() function:

You can use seq() function to create a vector within an interval by specifying step size or specifying length of the vector. 

seq(1:10) #By deafaut it will be incremented by 1
seq(1, 20, length.out=5) # specify length of the vector
seq(1, 20, by=2) # specify step size

Output:

> seq(1:10) #By deafaut it will be incremented by 1
[1] 1 2 3 4 5 6 7 8 9 10
> seq(1, 20, length.out=5) # specify length of the vector
[1] 1.00 5.75 10.50 15.25 20.00
> seq(1, 20, by=2) # specify step size
[1] 1 3 5 7 9 11 13 15 17 19

Extract Elements from a Vector:

Elements of a vector can be accessed using indexing. The vector indexing can be logical, integer or character. The [ ] brackets are used for indexing. Indexing starts with position 1 unlike most programming languages where index start from 0.

Extract Using Integer as Index:

We can use integers as index to access specific elements. We can also use negative integers to return all elements except that specific elements. 

x<- 101:110
x[1]   #access the first element
x[c(2,3,4,5)] #Extract 2nd, 3rd, 4th, and 5th elements
x[5:10]        #Extract all elements from 5th to 10th
x[c(-5,-10)] #Extract all elements except 5th and 10th
x[-c(5:10)] #Extract all elements except from 5th to 10th

Output:

> x[1] #Extract the first element
[1] 101
> x[c(2,3,4,5)] #Extract 2nd, 3rd, 4th, and 5th elements
[1] 102 103 104 105
> x[5:10] #Extract all elements from 5th to 10th
[1] 105 106 107 108 109 110
> x[c(-5,-10)] #Extract all elements except 5th and 10th
[1] 101 102 103 104 106 107 108 109
> x[-c(5:10)] #Extract all elements except from 5th to 10th
[1] 101 102 103 104

Extract Using Logical Vector as Index:

If you use a logical vector for indexing, the position where the logical vector is TRUE will be returned. 

x[x < 105]
x[x>=104]

Output:

> x[x < 105]
[1] 101 102 103 104
> x[x>=104]
[1] 104 105 106 107 108 109 110

Extract Using Character Vector as Index:

x<- c("R"=1,"Python"=2,"Java"=3)
names(x)
x["R"]
x[c("Java","Python")]

Output:

names(x)
[1] "R" "Python" "Java"
> x["R"]
R
1
> x[c("Java","Python")]
Java Python
3 2

Modify a Vector in R:

We can modify a vector assiging new value to it. You can truncate a vector by using reassignments. Check the below example. 

x<- 10:12
x[1]<- 101 #Modify the first element
x
x[2]<-102 #Modify the 2nd element
x
x<- x[1:2] #Truncate the last element
x

Output:

> x
[1] 101 11 12
> x[2]<-102 #Modify the 2nd element
> x
[1] 101 102 12
> x<- x[1:2] #Truncate the last element
> x
[1] 101 102

You can delete a vector by simply assigning a NULL to it.

x<- NULL

Output:

> x<- NULL
> x
NULL

Arithmetic Operations on Vectors:

You can use arithmetic operation in two vectors of same length. They can be added, subtracted, multiplied or divided. Check the output of the below code.

 

# Create two vectors.
v1 <- c(1:10)
v2 <- c(101:110)

# Vector addition.
add.result <- v1+v2
print(add.result)

# Vector substraction.
sub.result <- v2-v1
print(sub.result)

# Vector multiplication.
multi.result <- v1*v2
print(multi.result)

# Vector division.
divi.result <- v2/v1
print(divi.result)

Output:

> print(add.result)
[1] 102 104 106 108 110 112 114 116 118 120
> print(sub.result)
[1] 100 100 100 100 100 100 100 100 100 100
> print(multi.result)
[1] 101 204 309 416 525 636 749 864 981 1100
> print(divi.result)
[1] 101.00000 51.00000 34.33333 26.00000 21.00000 17.66667 15.28571 13.50000 12.11111 11.00000

How to Sort a Vector in R?

You can sort vectors using the sort() function. By default, it sorts in ascending order. To sort in descending order you can pass decreasing=TURE as parameter. 

 

x<- 110:101
x
y<-sort(x)
y
z<-sort(y, decreasing=TRUE)
z

Output:

y
[1] 101 102 103 104 105 106 107 108 109 110
> z<-sort(y, decreasing=TRUE)
> z
[1] 110 109 108 107 106 105 104 103 102 101
> x<- 110:101
> x
[1] 110 109 108 107 106 105 104 103 102 101
> y<-sort(x)
> y
[1] 101 102 103 104 105 106 107 108 109 110
> z<-sort(y, decreasing=TRUE)
> z
[1] 110 109 108 107 106 105 104 103 102 101

Find Minimum and Maximum in a Vector:

The minimum and the maximum of a vector can be found using the min() or the max() function. range() is also available which returns the minimum and maximum in a vector. 

x<- 1001:1010
max(x) # Find the maximum
min(x) # Find the minimum
range(x) #Find the range

Output:

> max(x) # Find the maximum
[1] 1010
> min(x) # Find the minimum
[1] 1001
> range(x) #Find the range
[1] 1001 1010

Find Where the Minimum or Maximum is Located in a Vector?

which.min(x) #Find index of minimum
which.max(x) #Find index of maximum
x[which.min(x)] #You can find the minimum value by using this also

Output:

> which.min(x) #Find index of minimum
[1] 1
> which.max(x) #Find index of maximum
[1] 10
> x[which.min(x)] #You can find the minimum value by using this
[1] 1001

R Data Structures

Mixing Objects