1. GETTING STARTED WITH R:
VARIABLES, VECTORS, MATRICES
Vladimir E. Bakhrushin,
Professor, D. Sc.
Vladimir.Bakhrushin@gmail.com
2. General information
R is an object-oriented programming language and
environment. Its distinguishing feature is the existence of a large
number of libraries for statistical data analysis.
R is a free software with open source. Another peculiarity is
that R can be used in all operating systems, particularly in
Windows, Linux, Mac OS, etc.
In 1997 the CRAN (Comprehensive R Archive Network
(http://cran.r-project.org) project was created. It is a repository
containing R system, libraries, materials and other related to R
resources.
4. Working Directory
By default, the program and data are stored and looking for in
the working directory. We can determine, which directory is the
working, with the command:
> getwd().
To change the working directory we can with the command:
> setwd(“…”)
In brackets it is necessary to specify the path to correct
directory.
In RGui it can also be done by selecting in the menu "File Change Folder".
5. Scripts
To create programs scripts are used. They can be written with
any text editor or in a special window that arises when selecting
in the main menu "File - New script".
To open existing scripts you must choose in the main menu:
"File - Open the script."
6. Vectors
The basic structure of R is a vector. Its elements are one or
more data values of the same type. Scalar value can be
represented as a vector containing a single element. More
complex structures – matrices, arrays, etc. can be created from
the vectors.
Vector elements may belong to such data types: numeric,
integer, character, complex, logical, raw etc.
NaN is the result of calculations caused indeterminate forms
0/0, ∞/∞, ∞ − ∞ etc.
NA is used as a symbol of uncertain at the moment value
(analog of NULL for database management systems).
9. Some functions for operating with
vectors
sort(x)
var(x)
mean(x)
sum(x)
Functions
prod(x)
max(x)
min(x)
length(x)
10. Matrices
Any vector can be transformed into a matrix by specifying the
way of its elements placing in this matrix.
Function m=matrix(data = y, nrow = n, ncol = m) places the
elements of vector y into matrix m of nm dimension so that
initially the first column is filled from top to bottom, then the
second, etc.
If the vector length is lower then nm, its elements are used
again, starting with the first. If the length of the vector is more
than nm, then some of its elements remain unused .
It is possible to combine two vectors into the matrix using the
function c().
17. Arrays
Vectors and matrixes are special cases of arrays having,
respectively, one or two dimensions.
In general, to create an array function array(<data vector>,
<dimension vector>) is used.
<Data vector> is the vector of numbers from which the array
is formed.
<Dimension vector> is a vector of length one or more giving
the number of dimensions and maximal indices in each
dimension.