1. Introduction

In the last two articles we have learned how to install and run GNU R on the Linux operating system. The purpose of this article is to provide a quick reference tutorial to GNU R that contains introduction to the main objects of the R programming language . We will learn about basic operations in R, functions and variables. Moreover, we will introduce R data structures, objects and classes.

2. Basic Operations in R

Let us start with a simple mathematical example. Enter, for instance, addition of seven and three into your R console and press enter, as a result we obtain:

> 7+3[1] 10

To explain in more detail what just happened and what is the terminology we use when running R, we say that the R interpreter printed an object returned by an expression entered into the R console. We should also mention that R interprets any number as a vector. Therefore, "[1]" near our result means that the index of the first value displayed in the given row is one. This can be further clarified by defining a longer vector using the c() function. For example:

It is also possible to perform operations on vectors. For instance, we can add two vectors as follows:

> c(1,5,67,0)+c(0,1.5,6.7,3)[1] 1.0 6.5 73.7 3.0

Note that this means adding corresponding elements of these two vectors. If the vectors are not the same size then the shorter vector is repeated multiple times and if the longer object length is not a multiple of the shorter object length a warning message is produced:

3. Functions and Variables

We can also define or use predefined functions in R. Most functions in R are constructed in the following form

f(argument1, argument2,...)

Here "f" is the name of the function and "argument1, argument2,..." is the list of arguments to the function. For example, using some predefined functions we obtain

> sin(pi/2)[1] 1> log(3)[1] 1.098612

In contrast to the above example, some functions in R are in the form of operators like addition, power, equality, etc. For instance, the equality operator produces a Boolean data type outcome (FALSE/TRUE):

> 4==4[1] TRUE

Similarly as in other programming language R uses variables. The assignment operator is here "<-" (or "="), for instance

> x<-c(1,4,7)> x+x[1] 2 8 14

We can now refer to the third value of the vector "x" by

> x[3][1] 7

or fetch only members less than seven:

> x[x<7][1] 1 4

We can also, for instance, fetch items one and three as

> x[c(1,3)][1] 1 7

Finally, you can define functions in R by simply naming them accordingly and then calling them with this name similarly to the build in R functions. For example:

> myfunction<-function(x,y){x+y}> myfunction(4,5)[1] 9

If you would like to see the code corresponding to a given function simply type in the name of the function as

> myfunctionfunction(x,y){x+y}

4. Data Structures

As a first example of a data structure we illustrate how to define matrices (arrays), that is multidimensional vectors.

Referring to a value of a part of an array is again simple, for instance

> w[1,1,1][1] 1> w[1:2,1:2,1] [,1] [,2][1,] 1 4[2,] 2 5

By omitting the indices we obtain all elements of a given dimension, such as:

> w[,1,1][1] 1 2 3

Let us now look at more complicated data structures with more then one underlying data type. These data types are called lists. Lists in R may contain a various selection of objects of various data type. We can name each component of a list, and therefore, we can later refer to that component by its name or location. For example,

We can now refer to the components of the list by name or by location as indicated below

> l$visitors[1] "10,000"> l[1]$name[1] "linuxcareer.com"

> l[[1]][1] "linuxcareer.com"

A data frame is a list that contains multiple named vectors with the same lengths. It is similar structure to a database. Let us now construct a data frame that contains some exchange rates (other currency/USD):

5. Objects and Classes

R is an object oriented programming language. This means that every object in R has a type and is a member of a class. To identify a class for a given object we use the function class() as in the following example:

In R not all functions are associated with a particular class as in other object oriented programming languages. However, there exist some functions that are closely linked with a specific class. These are called methods. In R methods called generic functions share the same name for different classes. This allows such generic functions to be applied to objects of different types. For instance, "-" is a generic function for subtracting objects. You can subtract numbers but you can also subtract number from a date as below:

> 4-2[1] 2> as.Date("2012-09-08")-2[1] "2012-09-06"

6. Conclusion

The aim of this basic R tutorial was to introduce R programming language to beginners, who never used R before. This tutorial may also be useful as a reference tutorial for those who will learn more advanced applications of the R statistical software. In the next article we will describe how to define statistical models and perform basic statistical analysis with R. This will be combined with illustration of graphical possibilities of the R software.

Stay Tuned

Make sure you tune in to our Newsletter and Linux IT jobs portal to stay informed about the latest opportunities in the field. To help you to keep your skills sharp we also offer various Linux Training courses. Lastly, visit our Linux Forum if you want to share your Linux experiences with us or require additional help.

Author: Renata Rendek

I was introduced to the GNU/Linux operating system when writing my PhD thesis. Since then I have used Linux programming and software for my research work. I come from a mathematical background and I am interested in writing about statistical open source software such as R and Octave, which will promote the use of GNU/Linux in financial and scientific disciplines.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.