Computations
MATHEMATICAL FUNCTIONS
The following functions apply to either an scalar or to a vector or
matrix, element by element
Usual operations functions:
a+b, a-b, a*b, a/b, a^b, is a vector with the sum (or whatever)
of the coordinates of a and b.
To get this in a new vector, we can do d_a+b or d<-a+b.
These operation apply to vectors and matrices doing these operation by
coordinates.
Trigonometric functions:
cos, sin, tan, acos, asin, atan, cosh, sinh, tanh, acosh, asinh, atanh
Exponential functions:
gamma, lgamma,
exp, log, log10, log(x, base=)
Other mathematical functions:
sqrt (square root), abs returns the absolute value,
Truncations and rounding functions:
- trunc() returns the integer part.
- ceiling() returns the next integer.
- round() rounds to the nearets integer. values 1/2 are
rounded to the nearest even integer
- round(x,,"positive integer")) rounds using the specified number of decimal places.
- floor() returns the previous to the next integer.
- signif(x,"positive integer")) rounds data to
the specified number of significant digits. All the numbers are
printed to the same format. 0's are added where necessary.
- x%/%y = floor(x/y) returns the integer part of x/y.
- x%% y =x- (x%/%y)*y returns x modulus y.
For example,
> trunc(x)
[1] -1 -1 -5 0 0 1 2 2
> x_c(-1.7,-1.3,-5,.2,.5,1.5,2.3,2.7)
> round(x)
[1] -2 -1 -5 0 0 2 2 3
> floor(x)
[1] -2 -2 -5 0 0 1 2 2
> ceiling(x)
[1] -1 -1 -5 1 1 2 3
> signif(x,1)
[1] -2.0 -1.0 -5.0 0.2 0.5 2.0 2.0 3.0
> x_c(-24,-99,82,15)
> x%/%10
[1] -3 -10 8 1
> x%%10
[1] 6 1 2 5
OPERATIONS IN VECTORS
The following are operations in one vector giving a number:
- sum finds the sum of the all elements of the vector.
- prod finds the product of the all elements of the vector.
- max finds the maximum of the all elements of the vector.
- min finds the minimum of the all elements of the vector.
For example:
> x
[1] -1.7 -1.3 -5.0 0.2 0.5 1.5 2.3 2.7
> sum(x)
[1] -0.8
> prod(x)
[1] -10.29308
> max(x)
[1] 2.7
> min(x)
[1] -5
The following are operations in one vector giving another vector:
- cumsum(x)
finds the cumulative sums of the all elements of the vector.
- diff(x) finds the differences: x[i+1]-x[i] of the elements of the vector.
- diff(x,lag="integer") finds
ds the differences: x[i+lag]-x[i]
of the elements of the vector.
- cumprod finds the product of the all elements of the vector.
For example:
> x
[1] -1.7 -1.3 -5.0 0.2 0.5 1.5 2.3 2.7
> cumsum(x)
[1] -1.7 -3.0 -8.0 -7.8 -7.3 -5.8 -3.5 -0.8
> cumpro(x)
Problem: Couldn't find a function definition for "cumpro"
> cumprod(x)
[1] -1.70000 2.21000 -11.05000 -2.21000 -1.10500 -1.65750 -3.81225
[8] -10.29308
> diff(x)
[1] 0.4 -3.7 5.2 0.3 1.0 0.8 0.4
The following are operations in two vectors giving a number:
- crossprod(x,y) finds the crossproduct of x and y.
The following are operations in two vectors giving another vector:
- pmax(x,y) finds the maximum of every element of x and y.
- pmin(x,y) finds the minimum of every element of x and y.
MATRICES
The following functions apply specifically to matrices:
- t(A) finds the transpose of A.
- A%*%B multiply the matrices A and B.
- diag(A) finds diagonals of A.
- crossprod(A,B) finds the cross product of A and B.
- outer(A,B) finds the outer product A and B.
- svd(A) singular value decomposition.
- qr(A) QR decomposition
- solve(A,B) finds the solution to the system of equations
A %*% X = B.
- solve(A) finds the inverse of A.
- eigen(A) eigenvalues.
- diag(A) find a vector with the diagonals of the matrix A
- diag(x) find a matrix with the elements of x in the
diagonals
- chol(A) Choleski decomposition.
- eigen(A) creates an object of mode list with two components:
$values and $vectors. Alternatively, eigenvalues could have been obtained using eigen(A)$values,
and eigenvectors using eigen(A)$vectors
APPLY AND SWEEP
The function apply allows to use vector commands either by rows or by columns. The first argument of the command apply is the matrix or data frame to which the function will be applied. The second argument gives the dimensions over which the function is to be applied. In the case of a matrix, 1 indicates rows, 2 indicates columns. The third argument gives the name of the function to be applied.
The function sweep(A,integer,a) substract the vector a, by columns if integer=1 and by rows if integer=2, from the matrix A.
> A
Height Weight
Tom 42 66
Fred 40 62
Sue 42 71
Donna 43 79
Lee 42 67
> apply(A,1,mean)
Tom Fred Sue Donna Lee
54 51 56.5 61 54.5
> apply(A,2,mean)
Height Weight
41.8 69
> a2_c(1:10)
> a2
[1] 1 2 3 4 5 6 7 8 9 10
> sweep(A,2,a2)
Height Weight
Tom 41 64
Fred 37 58
Sue 37 65
Donna 36 71
Lee 33 57
> sweep(A,2,a2,"+")
Height Weight
Tom 43 68
Fred 43 66
Sue 47 77
Donna 50 87
Lee 51 77
> sweep(A,1,a2,"*")
Height Weight
Tom 42 396
Fred 80 434
Sue 126 568
Donna 172 711
Lee 210 670
Comments to:
Miguel A. Arcones
Go to main homepage: