Friday, 10 July 2015

R : Vectorized Operations


In vectorized operations, an operation is applied to a vector, is actually applied individually to each element.
> x <- 1:5
> y <- 6:10
> 
> x
[1] 1 2 3 4 5
> y
[1]  6  7  8  9 10
> 
> x+y
[1]  7  9 11 13 15
> 
> x-y
[1] -5 -5 -5 -5 -5
> 
> x>2
[1] FALSE FALSE  TRUE  TRUE  TRUE
> 
> x>y
[1] FALSE FALSE FALSE FALSE FALSE
> 
> 5*x
[1]  5 10 15 20 25

Observe above snippet,

5*x multiplies every element in the vector x by 5.

x+y adds every element in vector x with corresponding positioned element in vector y.

x>2

Compare every element in vector with 2.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment