R TUTORIAL, Part I: Derivatives

R is a statistical analyzation language, and that means it is very good at data manipulation and data analyzation. One key way to analyze data is through plotting, and R excels in this field.

\[ y' = f(x,y), \qquad y(x_0 ) = y_0 . \]

Derivatives

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

=========================== to be checked ===================

R operates on named data structures. The simplest such structure is the numeric vector, which is a single entity consisting of an ordered collection of numbers. To set up a vector named v, say, consisting of five numbers, namely 1.5, 3.1415926, 3.17, 4.55 and 31.8, use the R command

This is an assignment statement using the function c() that in this context can take an arbitrary number of vector arguments and whose value is a vector got by concatenating its arguments end to end. A number occurring by itself in an expression is taken as a vector of length one.

Notice that the assignment operator (‘<-’), which consists of the two characters ‘<’ (“less than”) and ‘-’ (“minus”) occurring strictly side-by-side and it ‘points’ to the object receiving the value of the expression. In most contexts the ‘=’ operator can be used as an alternative.

Assignment can also be made using the function assign(). An equivalent way of making the same assignment as above is with:

Assignments can also be made in the other direction, using the obvious change in the assignment operator. So the same assignment could be made using
If an expression is used as a complete command, the value is printed and lost. So now if we were to use the command
the square of reciprocals of the five values would be printed at the terminal (and the value of x, of course, unchanged).

The further assignment

would create a vector y with 11 entries consisting of two copies of x and x² with a zero in the middle place.

Vectors can be used in arithmetic expressions, in which case the operations are performed element by element. Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression. Shorter vectors in the expression are recycled as often as need be (perhaps fractionally) until they match the length of the longest vector. In particular a constant is simply repeated. So with the above assignments the command

The elementary arithmetic operators are the usual +, -, *, /, and ^ or raising to a power. In addition all of the common arithmetic functions are available. Also some common operations are applicable for vectors. The range command gives 2-vector output, namely c(min(x), max(x)).
sort(x) returns a vector of the same size as x with the elements arranged in increasing order; however there are other more flexible sorting facilities available (see order() or sort.list()).

Generate vector of n = 5 (for example) uniform random vals between 0 and 1

To build the vector [2 3 4 5 6 7] with integer entries, type:
To build a vector [7 6 5 4 3 2] enter
To build a vector [3 6 9 12 15 18] with shift of 3 units enter
Build a vector containing n (wen = 6 for instance) take equally-spaced values between ab inclusive
Build a vector containing n logarithmically equally-spaced values between 10a and 10b inclusive
Build a vector of length n containing all zeros
Build a vector of length n containing the numerical value n in all positions
Access an element of vector v
R does not have a norm function for vectors; only one for matrices. But the following will work:
norm(matrix(v),’1’) 
for 1-norm \( \| {\bf v} \|_1 , \)
norm(matrix(v),’i’)
for infinity-norm \( \| {\bf v} \|_{\infty} = \max_i \left\{ |v_i | \right\} , \) and
sum(abs(v)^p)^(1/p)
for p-norm \( \| {\bf v} \|_{p} = \left( \sum_i |v_i |^p \right)^{1p} . \)

Compute mean of all elements in vector v

mean(v)
Compute standard deviation of all elements in vector v
sd(v)
Compute variance of all elements in vector v
var(v)
Vector dot product \( \displaystyle {\bf x} \cdot {\bf y} = {\bf x}^{\mathrm T} {\bf y} : \)
sum(x*y)

 

Example: The paraboloid example shows that any function with three variables can be visually represented using the plot and plotly functions.

Next, we utilize anothe rpackage plotly for creating interactive web-based graphs via the open source JavaScript:

Rgl is another package that is useful for 3d plotting. It is interactive; however, mac users must download XQuartz to use the rgl package: https://www.xquartz.org
     ■

 

Example: We plot a torus with the follwoing commands:

     ■

 

  1. Schiesser, W.E., An Introductory Comparison of Matlab and R, Lehigh University, Bethlehem, USA.
  2. Soetaert, K, Cash, J., Mazzia, F., Solving Differential Equations in R, 2012, Springer, Heidelberg New York Dordrecht London