R Programming/Maths
From Wikibooks, the open-content textbooks collection
- Help for simple maths :
?Arithmetic
- Power function
> 10^3 [1] 1000 > 10**3 [1] 1000
- log / exp
> log(2.71) [1] 0.9969486 > log10(10) [1] 1 > exp(1) [1] 2.718282
- Modulo and integer division (i.e. euclidean division)
> 5%%2 [1] 1 >5%/%2 [1] 2
- Factorial
> factorial(3) [1] 6 > prod(1:3) [1] 6
- The number of combination of length k within n numbers :

choose(3,2)
- Union and intersection
> union(1:10,5:7) [1] 1 2 3 4 5 6 7 8 9 10 > intersect(1:10,5:7) [1] 5 6 7
Contents |
[edit] Matrices
If you want to create a new matrix, one way is to use the matrix() function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option). Here are two examples.
> matrix(data = NA, nrow = 5, ncol = 5, byrow = T)
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] NA NA NA NA NA
[3,] NA NA NA NA NA
[4,] NA NA NA NA NA
[5,] NA NA NA NA NA
> matrix(data = 1:15, nrow = 5, ncol = 5, byrow = T)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 1 2 3 4 5
[5,] 6 7 8 9 10
> v1 <- 1:5
> v2 <- 5:1
> v2
[1] 5 4 3 2 1
> cbind(v1,v2)
v1 v2
[1,] 1 5
[2,] 2 4
[3,] 3 3
[4,] 4 2
[5,] 5 1
> rbind(v1,v2)
[,1] [,2] [,3] [,4] [,5]
v1 1 2 3 4 5
v2 5 4 3 2 1
The dimension of a matrix can be obtained using the dim() function or alternatively nrow() and ncol()
> dim(X) [1] 5 5 > nrow(X) [1] 5 > ncol(X) [1] 5
- Inner product X%*%Y
- Kronecker product X%x%Y
- Outer Product X%o%Y
[edit] Matrix operators
- Invert a matrix
> M <- cbind(c(1,0,1),c(0,1,2),c(0,0,1))
> solve(M)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] -1 -2 1
> solve(M)%*%M
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
- Extract the diagonal
> diag(M) [1] 1 1 1
- Transpose the matrix
> t(M)
[,1] [,2] [,3]
[1,] 1 0 1
[2,] 0 1 2
[3,] 0 0 1
- Eigenvalues and eigenvectors
> eigen(M)
$values
[1] 1 1 1
$vectors
[,1] [,2] [,3]
[1,] 0 2.220446e-16 0.000000e+00
[2,] 0 0.000000e+00 1.110223e-16
[3,] 1 -1.000000e+00 -1.000000e+00
> b=matrix(nrow=2,ncol=2,c(1,2,3,4))
> a=matrix(nrow=2,ncol=2,c(1,0,0,-1))
> a
[,1] [,2]
[1,] 1 0
[2,] 0 -1
> b
[,1] [,2]
[1,] 1 3
[2,] 2 4
> a%*%b
[,1] [,2]
[1,] 1 3
[2,] -2 -4
> b%*%a
[,1] [,2]
[1,] 1 -3
[2,] 2 -4
>
[edit] Solving a linear equation
> m=matrix(nrow=2,ncol=2,c(1,-.8,1,.2))
> m
[,1] [,2]
[1,] 1.0 1.0
[2,] -0.8 0.2
>
> l=matrix(c(1.0+25.0/18,25.0/18.0))
> l
[,1]
[1,] 2.388889
[2,] 1.388889
>
> k=solve(m,l)
> k
[,1]
[1,] -0.9111111
[2,] 3.3000000
>
> m%*%k #checking the answer
[,1]
[1,] 2.388889
[2,] 1.388889
>
[edit] Numerical approximation
[edit] Derivation
- numDeriv package
[edit] Integration
R can perform one dimensional integration. For example we can integrate over the density of the normal distribution between
and 
> integrate(dnorm,-Inf,Inf) 1 with absolute error < 9.4e-05 > integrate(dnorm,-1.96,1.96) 0.9500042 with absolute error < 1.0e-11 > integrate(dnorm,-1.64,1.64) 0.8989948 with absolute error < 6.8e-14 # we can also store the result in an object > ci90 <- integrate(dnorm,-1.64,1.64) > ci90$value [1] 0.8989948 > integrate(dnorm,-1.64,1.64)$value [1] 0.8989948
see the adapt package for multivariate integration.
> library(adapt)
> ?adapt
> ir2pi <- 1/sqrt(2*pi)
> fred <- function(z) { ir2pi^length(z) * exp(-0.5 * sum(z * z))}
>
> adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred)
value relerr minpts lenwrk ifail
1.039222 0.0007911264 231 73 0
> adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred, eps = 1e-4)
value relerr minpts lenwrk ifail
1.000237 1.653498e-05 655 143 0
> adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred, eps = 1e-6)
value relerr minpts lenwrk ifail
1.000039 3.22439e-07 1719 283 0
[edit] Symbolic calculus
R can give the derivative of a function.
> D(expression(x^n),"x") x^(n - 1) * n > D(expression(exp(a*x)),"x") exp(a * x) * a > D(expression(1/x),"x") -(1/x^2)
> D(expression(x^3),"x") 3 * x^2
[edit] Solving polynomial equations
To solve
, where
are given numbers, use the command
polyroot(c(n,...,b,a))
So, for example, to calculate the roots of the equation 2x2 − 5x − 3 = 0 one would do as follows:
> polyroot(c(-3,-5,2)) [1] -0.5+0i 3.0-0i
and the solution can be read to be 
[edit] See also
The following command gives help on special mathematical functions related to the beta and gamma functions.
?Special