R – Vectors

R Vector data type is same as Array of other programming languages containing similar type of element.

These are of 6 types:

  • Logical
  • Integer
  • Double
  • Character
  • Complex
  • Raw

Type of a vector can be checked with the function typeof ().

Length of a vector which is the number of elements present in a vector can be checked with length () function.

Examples:

> x <- c(TRUE, FALSE)

> typeof(x)

[1] “logical”

> length(x)

[1] 2

> x <- c( 1.5 ,0.32, 5, 4)

> typeof(x)

[1] “double”

> length(x)

[1] 4

> x <- c( “Amit”, “S”)

> typeof(x)

[1] “character”

> x <- c( 1+0i ,0)

> typeof(x)

[1] “complex”

> length(x)

[1] 2

> x <- 1:5

> typeof(x)

[1] “integer”

> length(x)

[1] 5

> x <- raw(3)

> typeof(x)

[1] “raw”

> length(x)

[1] 3

  • Raw vector is used to store fixed length sequence of bytes

First element of a vector in R is indexed as 1 unlike 1st element in an array of other programming language which is indexed as zero

> x <- c( 1.5 ,0.32, 5, 4)

> x[1]

[1] 1.5

Merging Vectors

2 vectors can be merged in to a new vector containing element of each vectors

> x <- c(1, 2, 3)

> Y <- c(“Amar”, “Akbar”, “Anthony”)

> c(x, Y)

[1] “1”       “2”       “3”       “Amar”   “Akbar”   “Anthony”

Click here to read more about R Vectors Manipulation