Computations


[Mathematical functions | Vectors | Matrices| Apply and sweep]


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:

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:

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:

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:

The following are operations in two vectors giving another vector:

MATRICES

The following functions apply specifically to matrices:

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: