



Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!




Contents
1.1. R is a calculator.
(a) Operators: <, >, <=, >=,! =, !(Not), | (or), &(And), == (compare)
(b) Arithmetic operations: +, −, ∗, /.
(c) Log and Exp: log (natural logarithm), log(x, b)(logb x) and exp(x)(ex).
(d) Trigonometric functions: cos(x), sin(x), tan(x), arccos(x), arcsin(x), arctan(x) with x, y in rad.
1.2. Defining variables and assigning values. In order to define/assign values to a variable, use ”=” or < −.
Let us see how to assign values using < and −.
x < − 3
or
3 < − x
code
x < − 3 x [1] 3
1
In the OutPut line above, [1] indicates that the number which follows is the first element in x (here it is obvious, since x is a scalar quantity; later we will see how it works with vectors).
In order to create a vector with components (3, 6 , 8 , 9), write
x < − c(3, 6 , 8 , 9)
code
x < − c(3, 6 , 8 , 9) x [1] 3 6 8 9
If the vector is longer than the allowed space for a line, each new line will start with [n], where n indicates the place in the vector of the following entry.
In order to write a cleaner code, if we end the command line with a semicolon (; ) R will perform the computations but will not show the output result, e.g., writting x = 5^2 ; you will not see any output lines (even if the value of x has bee set to 5^2 ).
x = 5^2 ; x [1] 25
R does not show the result, but records the value for the variable x.
It is also possible to introduce several orders in the same command line, splitting it with semicolons, according to if we want to see the results as output or not.
1.3. R is a scientific calculator. R allows us to use function (either predefined or new ones defined by the user). Let us set a = cos π 3 , b = sin π 3 and check that a^2 + b^2 = 1:
a = cos(pi/3); b = sin(pi/3); a^2 + b^2 [1] 1 a [1] 0. 5 b [1] 0. 8660254
All elementary functions are already implemented in R.
1.4. R is a matrix calculator. R is a calculator which works with matrices. If we want to add matrices, (^) [ 1 0 − 1 0 1 2
what shall we do?
Since a vector is just a column matrix, in order to save the vector (1, 2 , 3 , 4 , 5) we could write
c(1, 2 , 3 , 4 , 5) [1] 1 2 3 4 5
also, we could use
1 : 5 [1] 1 2 3 4 5
to generate integer numbers from 1 to 5
seq(1, 5) [1] 1 2 3 4 5
seq(1, 5 , by = 0.5) [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5. 0
In all three cases we have generated numbers from 1 to 5. In the second case, we add at each step 0.5 until we reach 5.
rep(1, 5) [1] 1 1 1 1 1
rep(c(1, 2), 5) [1] 1 2 1 2 1 2 1 2 1 2
rep(1 : 4, 2) [1] 1 2 3 4 1 2 3 4
rep(1 : 3, c(1, 4 , 5)) [1] 1 2 2 2 2 3 3 3 3 3
In the first box we have repeated 1 five times. In the second one, we repeated the pattern (1, 2) five times. Thirdly, we repeated the sequence 1, 2 , 3 , 4 twice. Finally, the sequence 1 , 2 , 3 has been repeated according to a pattern given by the vector (1, 4 , 5), i.e., 1 once, 2 four times, and 3 five times.
2.2. Arithmetic operations.
Addition of matrices: Addition of matrices of the same order uses operators + and −.
Multiplication of a scalar times a matrix: Multiplication uses the symbol ∗.
Product of matrices: Product of compatible matrices is done using the symbol % ∗ %.
Power of matrices: R does not implement a function to compute the n-th power of a matrix. In spite of this, the package expm includes the operator % f % to solve this deficiencly. It is very simple to use: if we want to compute An^ it is enough the use the expression A%%n. However, in order to do so, first we need to install the package expm.
Let us see an example. Try to write the input matrices
and compute A + B, 5A, D = A · C y (B + C) · D^4.
When we use wrong syntax, or ask for mpossible operations, the R output will show an error message.
a + b Error in a + b : non-conformable arrays
Here, addition is not allowed since matrices have different orders (different number of rows/columns).
We will use some packages which include useful functions and definitions. In order to download/install these packages we go to
Tools → Install Packages → matlib
Once the package is download, it will appear in the right-down box.
In the following http address, we can download a file containing all functions predefined in ”matlib”.
https://github.com/friendly/matlib
Seme for the package ”pracma”. Some useful functions found in these packages are...
3.4. Block matrices. We can bind different matrices to create new ones, eg., if
(^) y C =
we can construct
, y
Con R, para
m = cbind(b, c) m [, 1] [, 2] [, 3] [, 4] [1, ] 1 − 1 1 4 [2, ] 2 1 0 6 [3, ] 0 1 3 2
and
p = rbind(b, c) p [, 1] [, 2] [1, ] 1 − 1 [2, ] 2 1 [3, ] 0 1 [4, ] 1 4 [5, ] 0 6 [6, ] 3 2