http://how-to.linuxcareer.com/an-overview-of-gnu-r-programming-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

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.

# 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<-100 br="">> if (1==1) 1 else 0 [1] 1 > 100/5 [1] 20R 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.

> "LinuxCareer.com";sin(pi);5^7 [1] "LinuxCareer.com" [1] 1.224647e-16 [1] 78125

# 3. Objects

An R**object**can be thought of as an elementary component ("thing") of R programming language. For instance, the R objects are:- numeric vector;
- character vector;
- list;
- function.

**Examples of objects in R:**> c(1,5,2,7,9,0) [1] 1 5 2 7 9 0 > c("GNU R programming tutorial","LinuxCareer.com") [1] "GNU R programming tutorial" "LinuxCareer.com" > list("GNU R programming tutoial",c(1:5),"this is also an object in R") [[1]] [1] "GNU R programming tutoial" [[2]] [1] 1 2 3 4 5 [[3]] [1] "this is also an object in R" > function(a,b) {a/b} function(a,b) {a/b}

# 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<-3 br="">> y<- pre="" tutorial=""> Above, x and y are symbols.

## 5. Functions

Afunctionin R is an object that takes as arguments other objects and returns an object as a result. Did you know that the assignment operator '<- a="" can="" function="" in="" instead="" is="" of="" p="" r="" writing="" you="">> a<-1 pre=""> call '<- a="" and="" arguments="" as="" below="" function="" indicated="" p="" with="">> '<- a="" pre=""> Some examples of functions in R include:

- '<- assignment="" li="" operator="">
- '+' summation;
- 'if' statement;
- '[' vector reference.

**Examples of functions in R:**> '+'(1,1) [1] 2 > 'if'(1>3,"one greater than three", "one less than three") [1] "one less than three" > '['(a,1) [1] 1

# 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<-function i="" pre="" x=""> Let's see what happens if we define a vector 'w' and pass it as argument to the function 'f'.

> w<-c br="">> f(w,1) > w [1] 1 3 6 7What 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<-function br="" x="" y="">> functionname function(x,y) x+y## 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<-c br="">> typeof(x) [1] "integer"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]<-4 .1="" br="">> typeof(x) [1] "double"

## 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.> lapply(quote(if(1>3) "one is greater than three" else "one is less than three"),typeof) [[1]] [1] "symbol" [[2]] [1] "language" [[3]] [1] "character" [[4]] [1] "character"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.

## No comments:

## Post a Comment