This markdown assumes that reader is familiar with basics of vectors in R and here we only play around named vectors in R.

Unnamed Numeric Vector

Create a Numeric Vector obj of length 5

obj <- 11:15

Named Numeric Vector obj

Named Numeric Vector (Create Names for the above R object obj)

names(obj) <- c("Num1", "Sum1", "Lum1", "Dum1", "Rum1")
obj
## Num1 Sum1 Lum1 Dum1 Rum1 
##   11   12   13   14   15

Length of obj

Number of elements in obj

length(obj)
## [1] 5

Structure of obj

How obj is stored

str(obj)
##  Named int [1:5] 11 12 13 14 15
##  - attr(*, "names")= chr [1:5] "Num1" "Sum1" "Lum1" "Dum1" ...

Access just the obj values without names

unname(obj)
## [1] 11 12 13 14 15

Access just names of the obj

names(obj) 
## [1] "Num1" "Sum1" "Lum1" "Dum1" "Rum1"

Access Elements

Based on Names

Access elements of obj based on names of obj

obj["Sum1"]
## Sum1 
##   12

Access multiple elements of obj based on names of obj

obj[c("Rum1","Lum1","Sum1")]
## Rum1 Lum1 Sum1 
##   15   13   12

Based on Indexes/Positions

Access elements of obj based on positive indexes/position

Accesses just the 3rd element of the obj with name

obj[3]
## Lum1 
##   13

Access multiple elements of obj based on positive indexes/position

Accesses just the 2nd and 5th element of the obj with names

obj[c(2,5)]
## Sum1 Rum1 
##   12   15

Access consecutive elements of obj based on indexes/position

obj[3:5]
## Lum1 Dum1 Rum1 
##   13   14   15

Access elements of obj based on negative indexes/position

Accesses all elements except 3rd element with names

obj[-3]
## Num1 Sum1 Dum1 Rum1 
##   11   12   14   15

Access multiple elements of obj based on negative indexes/position

Accesses all elements except 2nd and 5th element with names

obj[c(-2,-5)] 
## Num1 Lum1 Dum1 
##   11   13   14

Replacements

Replace names of obj for few elements

names(obj)[c(2,5)] <- c("Elephant", "Snake")
obj
##     Num1 Elephant     Lum1     Dum1    Snake 
##       11       12       13       14       15

Replace value of obj for few elements

obj[c(1,4)] <- c(3000, 420)
obj
##     Num1 Elephant     Lum1     Dum1    Snake 
##     3000       12       13      420       15

Arrangements

Arrange obj in specific order based on indexes/position

obj[c(2,3,1,5,4)]
## Elephant     Lum1     Num1    Snake     Dum1 
##       12       13     3000       15      420

Arrange obj in specific order based on names of obj

obj[c("Lum1", "Rum1", "Dum1", "Sum1", "Num1")]
## Lum1 <NA> Dum1 <NA> Num1 
##   13   NA  420   NA 3000

Reverse Order Named Vector

Arranges obj in reverse order based on position and not value of obj elements

rev(obj)
##    Snake     Dum1     Lum1 Elephant     Num1 
##       15      420       13       12     3000

Arrange obj in ascending order of values of elements

obj[order(obj, decreasing = FALSE)]
## Elephant     Lum1    Snake     Dum1     Num1 
##       12       13       15      420     3000

Arrange obj in descending order of values of elements

obj[order(obj, decreasing = TRUE)]
##     Num1     Dum1    Snake     Lum1 Elephant 
##     3000      420       15       13       12

Arrange obj in alphabetical order of names of “obj”

obj[sort(names(obj))]
##     Dum1 Elephant     Lum1     Num1    Snake 
##      420       12       13     3000       15

Missing Values

Replace obj values with Missing values

obj[c(2,4)] <- NA 
obj
##     Num1 Elephant     Lum1     Dum1    Snake 
##     3000       NA       13       NA       15

Number of NA’s in obj

table(is.na(obj))
## 
## FALSE  TRUE 
##     3     2