An overview of GNU R programming language


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.


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

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.

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


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","")
[1] "GNU R programming tutorial" ""           
> list("GNU R programming tutoial",c(1:5),"this is also an object in R")
[1] "GNU R programming tutoial"

[1] 1 2 3 4 5

[1] "this is also an object in R"

> function(a,b) {a/b}
function(a,b) {a/b}


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
> y<-"R tutorial"

Above, x and y are symbols.


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 ‘<-‘ is a function in R? You can instead of writing

> a<-1

call ‘<-‘ function with arguments ‘a’ and ‘1’ as indicated below

> '<-'(a,1)

Some examples of functions in R include:

  • ‘<-‘ assignment 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

Other properties of R language

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(x,i){x[i]<-4}

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

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

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.

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(x,y) x+y
> functionname
function(x,y) x+y

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.


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(1:10)
> 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
> typeof(x)
[1] "double"

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 > 3

[1] "one is greater than three"

[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] "symbol"

[1] "language"

[1] "character"

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


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.

GNU R tutorial series:

Part I: GNU R Introductory Tutorials:

  1. Introduction to GNU R on Linux Operating System
  2. Running GNU R on Linux Operating System
  3. A quick GNU R tutorial to basic operations, functions and data structures
  4. A quick GNU R tutorial to statistical models and graphics
  5. How to install and use packages in GNU R
  6. Building basic packages in GNU R

Part II: GNU R Language:

  1. An overview of GNU R programming language