Thursday, February 28, 2013

An overview of GNU R programming language

1. Introduction

The aim of this article is to provide an overview of the GNU R
programming language. It starts a series of articles devoted to
programming with R. Its objective is to present, in an organized and
concise manner, the elementary components of the R programming language.
It is designed to help you understand R code and write your own. It is
assumed that the reader has already some basic programming knowledge of
R. If you are not familiar with any of R features it is recommended that
you first read A quick GNU R tutorial to basic operations, functions and data structures.

2. Expressions

An R expression is an elementary component of R code. Expression in R can be:

assignment statement;

conditional statement;

arithmetic expression.

Examples of R expressions:

> y> if (1==1) 1 else 0
[1] 1
> 100/5
[1] 20-100>

R expression are constructed from objects or functions. It is common
to separate them with a new line, however, you can also separate
expressions with semicolons as below.

4. Symbols

A symbol in R is a variable name. So if you assign an object to a variable name you actually assigning an object to a symbol. An environment in R, then, is a set of such symbols that are created for a given purpose.Example of symbols in R:

> x> y
Above, x and y are symbols.

5. Functions

A function in R is an object that takes as arguments
other objects and returns an object as a result. Did you know that the
assignment operator '

6. Other properties of R language

6.1. Objects are immutable in R

In R objects are immutable. This means that R will copy the object
not just reference to the object. Consider the following example. We
define a function that sets the ith element of vector 'x' to 4 as below

> f
Let's see what happens if we define a vector 'w' and pass it as argument to the function 'f'.

> w> f(w,1)
> w
[1] 1 3 6 7-c>

What we just observed above is that the vector 'w' was copied when it
was passed to the function so that the function does not modify this
vector.

6.2. Everything in R is an object

Everything
in R is an object. Objects are not only defined to store data as in the
case of vectors, lists or other data structures. Other examples of
objects in R are functions, symbols or R expressions. For instance,
function names in R are symbol objects that point to function objects as
indicated below

> functionname> functionname
function(x,y) x+y-function>

6.3. Special values in R

There is a number of special values used in R. These are:

NA, used to represent missing values, means "not available";

Inf and -Inf, resulting in a calculation when the output number is too big or too small or when dividing by zero;

NaN, resulting in a calculation that is not possible to compute such as division of zero by zero, means "not a number";

NULL, used often as an argument in functions, means that no value was assigned to that argument.

6.4. Coercion

R often coerces values from one type to another. For instance, when
you call a function with an argument of a wrong type, R will try to
convert this argument to a different type so the function can work.
Another example might be when we define a vector with numeric values, R
will assign it a type "integer" as below

> x> typeof(x)
[1] "integer"-c>

Now, when we change the forth element of vector 'x' to four, R will
automatically change the type of the vector to 'double' as indicated
below

> x[4]> typeof(x)
[1] "double"-4>

6.5. The R interpreter

An interpreter is a program that executes the
written code. There is no need to compile R code into an object language
as in the case of C, C++ or Java. This means that R is an interpreted
language.
R interpreter evaluates R expressions in few steps. First, it parses
an expression changing it into an appropriate functional form. Let's
call the quote() function to see how this happens.

> typeof(quote(if(1>3) "one is greater than three" else "one is less than three"))
[1] "language"

The R expression above returned a "language" object. To see how R evaluates an expression we produce a parse tree.

> as(quote(if(1>3) "one is greater than three" else "one is less than three"),"list")
[[1]]
`if`
[[2]]
1 > 3
[[3]]
[1] "one is greater than three"
[[4]]
[1] "one is less than three"

Let's also apply the typeof() function to the elements in such list, which shows how the expression is interpreted by R.

As you can see some parts of the if statement where not included in the parsed expression. That is, the else element. Additionally, it is interesting to note that the first item in the list is a symbol, which points to the if() function. Even though the syntax for the if
statement differs from the function call, the R interpreter translates
the expression into the function call with the name of the function as
its first argument and other arguments as in the list above.

7. Conclusion

This article is an introduction to the R programming language. In the
forthcoming articles we will focus in detail on the defined here
elements of the R language.
Make sure you tune in to our RSS and Linux jobs portal to stay informed about the latest opportunities in the field. Also visit our Linux Forum if you want to share your Linux experiences with us or require additional help.
-function>
->
->-1>
->->-3>