x <- 1:4
x * 2
## [1] 2 4 6 8
y <- 6:9
x + y
## [1] 7 9 11 13
something liek this:
x: 1 2 3 4
+ + + +
y: 6 7 8 9
---------------
7 9 11 13
http://swcarpentry.github.io/r-novice-gapminder/09-vectorization#challenge-1
comparison operators
x > 2
## [1] FALSE FALSE TRUE TRUE
** logical operators**
a <- x > 3 # or, for clarity, a <- (x > 3)
a
## [1] FALSE FALSE FALSE TRUE
TIP: any()
will return TRUE
if any element of a vector is TRUE
all()
will return TRUE
if all elements of a vector are TRUE
most functions also operate element-wise on vectors
functions
x <- 1:4
log(x)
## [1] 0.0000000 0.6931472 1.0986123 1.3862944
m <- matrix(1:12, nrow=3, ncol=4)
m * -1
## [,1] [,2] [,3] [,4]
## [1,] -1 -4 -7 -10
## [2,] -2 -5 -8 -11
## [3,] -3 -6 -9 -12
http://swcarpentry.github.io/r-novice-gapminder/09-vectorization/