> a <- c(7+4,7-4,7*4,7/4) # elemental arithmetic operations
> a
[1] 11.00 3.00 28.00 1.75
> length(a) # return vector length
[1] 4
> c(min(a),max(a)) # calculate minimum and maximum value of the vector
[1] 1.75 28.00
> which.min(a) # determine the location (index) of the minimum
[1] 4
> which.max(a) # determine the location (index) of the maximum
[1] 3
> sort(a) # sort vector values
[1] 1.75 3.00 11.00 28.00
> sum(a) # calculate sum of all vector values
[1] 43.75
> cumsum(1:10) # calculate cumulative sum
[1] 1 3 6 10 15 21 28 36 45 55
> cumprod(1:5) # calculate cumulative product
[1] 1 2 6 24 120 720 5040 40320
> mean(a) # calculate the mean value
[1] 10.9375
> median(a) # calculate the median value
[1] 7
> var(a) # calculate the variance
[1] 146.1823
> sd(a) # calculate the standard deviation
[1] 12.09059
> quantile(a, 0.25) # calculate first quantile (prob=25%)
25%
2.6875
There is a command to get basic statistical information in a simple way:
> summary(a)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.750 2.688 7.000 10.940 15.250 28.000
Some important mathematical functions are exp(), sin(), cos(), tan(), log(), log10(),...
> ?Trig # show information about trigonometric functions
> ?exp # help about 'exp()' function
R also includes Special functions of Mathematics: beta(a,b), gamma(x), ...
> ?Special # help about Special mathematical functions
Operations in R can be vectorized helping to improve the code readability and efficiency:
> a <- seq(10,30,10)
> b <- seq(1:3)
> a + b # makes the sum of two vectors
[1] 11 22 33
> a * b # vector product
[1] 10 40 90
> a / b # vector division
[1] 10 10 10
> a > 5 # logical operations
[1] TRUE TRUE TRUE
> b == 2
[1] FALSE TRUE FALSE
The vectorization can be also performed over matrices:
> m1 <- matrix(1:9, 3, 3) # 3 x 3 matrix definition
> m1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> m2 <- matrix(11:19, 3, 3) # 3 x 3 matrix definition
> m2
[,1] [,2] [,3]
[1,] 11 14 17
[2,] 12 15 18
[3,] 13 16 19
> m1 * m2 # element-wise matrix multiplication
[,1] [,2] [,3]
[1,] 11 56 119
[2,] 24 75 144
[3,] 39 96 171
> m1 %*% m2 # true matrix multiplication
[,1] [,2] [,3]
[1,] 150 186 222
[2,] 186 231 276
[3,] 222 276 330
Examples:
--- SHOW/HIDE ---