starting work with r

19
GETTING STARTED WITH R: VARIABLES, VECTORS, MATRICES Vladimir E. Bakhrushin, Professor, D. Sc. [email protected]

Upload: vladimir-bakhrushin

Post on 25-May-2015

265 views

Category:

Education


1 download

DESCRIPTION

Presentation on data analysis with R

TRANSCRIPT

Page 1: Starting work with R

GETTING STARTED WITH R: VARIABLES, VECTORS, MATRICES

Vladimir E. Bakhrushin,Professor, D. Sc.

[email protected]

Page 2: Starting work with R

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.

Page 3: Starting work with R

Starting message

Page 4: Starting work with R

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".

Page 5: Starting work with R

ScriptsTo 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."

Page 6: Starting work with R

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).

Page 7: Starting work with R

Creating vectors with function с()

Page 8: Starting work with R

Creating vectors with function seq()

Page 9: Starting work with R

Some functions for operating with vectors

Functions

min(x)

max(x)

prod(x)sum(x)

sort(x)

var(x)

mean(x)length(x)

Page 10: Starting work with R

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().

Page 11: Starting work with R

Creating matrixes

Page 12: Starting work with R

Creating matrices

Page 13: Starting work with R

Accessing the elements of vectors and matrices

Page 14: Starting work with R

Summation of vectors and matrixes elements

Page 15: Starting work with R

Transformation of vectors and matrices elements

Page 16: Starting work with R

Transformation of vectors and matrices elements

Page 17: Starting work with R

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.

Page 18: Starting work with R

Arrays

Page 19: Starting work with R

Arrays