Vector Data Type in R Programming

 Data Type:


Graphical Representation of Data Types in R is shown as below:


VECTOR

Vector Data Type is the Simplest Form of all other data types. Vector is Single Dimensional. It has 6 atomic data types.

Graphical View on Vector Data Type is as below
Vector is classified as below based on the number of the element it can store.

NOTE: Pink font Color indicates "Code". Blue font Color indicates "Output from R Console". Light Green Font Color indicates "Comment"

SINGLE ELEMENT VECTOR

#A.Logical

  v1 <- TRUE 

  print(v1)

[1] TRUE

  print(class(v1))

[1] "logical"

#B.Numeric

  v2<-45

  print(v2)

[1] 45

  print(class(v2))

[1] "numeric"

#C.Integer


Integer in R is recognized with suffix L.

  v3<-67L

  print(v3)

[1] 67

  print(class(v3))

[1] "integer"

#D.Complex

  v4<-2+3i

  print(v4)

[1] 2+3i

  print(class(v4))

[1] "complex"

#E.Character

  v5<-"E"

  print(v5)

[1] "E"

  print(class(v5))

[1] "character"

#F.Raw


Converts a length-one character string to raw bytes

  v6<-charToRaw("Auxilium")

  print(v6)

[1] 41 75 78 69 6c 69 75 6d

  print(class(v6))

[1] "raw"


Encoded Output from charToRaw can be converted back to original by using rawToChar function

rawToChar(v6, multiple = FALSE)

[1] "Auxilium"

MULTIPLE ELEMENT VECTOR

 Multiple Element created with c(),colon operator,sequence operator

  # Using c() function

  m1<-c(22.5,'a',TRUE,10) 

[1] "22.5" "a"    "TRUE" "10"  

  # Using colon operator


  v<-5:13  # It Generates Sequence from 5 to 13 and Stores in Variable "v". Step Value cannot be assigned here

  print(v)

[1]  5  6  7  8  9 10 11 12 13

  #Using Sequence Operator

  
v<-seq(2,10,by=2)  #It Generate Sequence of number with desired step value

  print(v)

[1]  2  4  6  8 10

OPERATION IN VECTOR DATA TYPE

1. Mean: 

Average of the Given Dataset

m=Sum of Terms/No of Terms

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

mean(vector1)

[1] 2.666667

2. Median:

Middle Value of the given Ordered Dataset

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

median(vector1)

[1] 2

3. Mode:

Most Common Value in the given Dataset

4. Range:

The Range is the difference between the maximum and minimum value in a given Dataset

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

range(vector1)

[1] 1  8

1 is the minumum value and 8 is the maximum value in the given vector1

5. Variance:

Variance measures how much data is spread out with relative to the mean

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

var(vector1)

[1] 4.424242

6. Standard Deviation

 It is a measure of the amount of variation or dispersion of a set of values.

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

sd(vector1)

[1] 2.103388

7. which.min

It returns minimum value in the given Dataset

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

which.min(vector1)

[1] 1

8. which.max

It returns maximum value in the given Dataset

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

which.max(vector1)

[1] 8

9. Repetition

It replicates or repeats the values in the given vector

rep(c(1,2,3), times = 3)

[1] 1 2 3 1 2 3 1 2 3

rep(c(2, 4, 2), each = 3)

[1] 2 2 2 4 4 4 2 2 2

rep(c(6, 13), times = c(4,2))

[1]  6  6  6  6 13 13

rep(1:3,length.out=7)     #R will repeat the vector until it reaches the specified length

[1] 1 2 3 1 2 3 1

#Repeating List

Groceries <- list(fruit=c("Apple","Orange"),Veg=c("Carrot","Beans") )

rep(Groceries, 2)

$fruit
[1] "Apple"  "Orange"

$Veg
[1] "Carrot" "Beans" 

$fruit
[1] "Apple"  "Orange"

$Veg
[1] "Carrot" "Beans"

10. Length

Returns the number of elements in the given vectors

vector1=c(1,2,3,1,2,4,5,2,1,1,8,2)

length(vector1)

[1] 12

11. Sorting

v4<-c(2,45,33,5,7,8,-1)

#Sorting number in ascending
v4.sort<-sort(v4)
print(v4.sort)
[1] -1  2  5  7  8 33 45

#Sorting number in descending
v4.sort<-sort(v4,decreasing = TRUE)
print(v4.sort)
[1] 45 33  8  7  5  2 -1

12. Accessing Vector Element

t<-c('a','b','c','d','e')

#Accessing using position
print(t[c(2,3,4)])
[1] "b" "c" "d"

#Accessing using logical indexing
print(t[c(TRUE,FALSE,TRUE,FALSE,TRUE)])
[1] "a" "c" "e"

#Accessing using negative indexing
print(t[c(-2,-5)])
[1] "a" "c" "d"

#Accessing using 0/1 indexing
print(t[c(0,1,1,0,0)])
[1] "a" "a"

13. Mathematical Operation

Vectors of the same length can be put into mathematical operations

v1=seq(5,25,by=5)
v2=seq(8,40,by=8)

#1. Addition
v3<-v1+v2
print(v3)
[1] 13 26 39 52 65

#2. Subtraction
v4<-v2-v1
print(v4)
[1]  3  6  9 12 15

#3. Multiplication
v5<-v1*v2
print(v5)
[1]   40  160  360  640 1000

#4. Division
v6<-v1/v2
print(v6)
[1] 0.625 0.625 0.625 0.625 0.625

14. Vector Element Recycling

If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.

v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11).i.e. elements are recycled to the length of v1
v3<-v1*v2
print(v3)
[1]  12  88  16  55   0 121

Comments

Post a Comment

Popular posts from this blog

Arrays in R Programming

Variables in R Programming