Saturday 13 June 2015

R : Vectors

Vector is similar to arrays in Java, it stores multiple copies of same data type. The ‘c’ function is used to create vectors in R.

Syntax

c(elem1, elem2 ….. elemN)

> c(1, 5, 7, 9, 8, 6, 4, 2)
[1] 1 5 7 9 8 6 4 2
> 
> class(c)
[1] "logical"
> 
> length(c(1, 5, 7, 9, 8, 6, 4, 2))
[1] 8

> vector1 <- c(1, 2, 3, 4)
> 
> vector1
[1] 1 2 3 4
> 
> length(vector1)
[1] 4
> 
> class(vector1)
[1] "numeric"

You can create vectors using vector() function also.
Syntax

vector(mode, length=val)
> vector2 = vector("numeric", length=10)
> 
> vector2
 [1] 0 0 0 0 0 0 0 0 0 0


By default, vector function initializes all values to zero for a numeric vector.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment