2. Task 2: Flash introduction to language R


 Action:
ação ícone

 Expected:
resultado ícone

Initial testes, try to write:

> 45
> 5*4
> "Hellow World"
> c(23, 56, 73)

 

The R language works like a command line console. To print a string, simply type the string, also in quotes. It's an interpreted language, so it doesn't have to be compiled, nor is there an executable file generated; the R program itself reads the instructions and executes them.

The last instruction above builds a vector of elements, which is returned.

 Action:
ação ícone

 Expected:
resultado ícone

> # assigning values to variables
> x=46
> y=5*4
> text="Hello World!"
> vector=c(23, 56, 73)
> aux
> x
> ls()
> x;y
> cat("Value of X: ", x, " Value of Y: ", y)

 

Comments in R start with # and continue to the end of the line. Notice the attempt to declare the aux variable, without the assignment. It doesn't make sense in R. Also note that the type of the variables is not specified. The ls() function lists all the objects that are defined. In this case it's only the x, y, text and vector objects, since the aux object hasn't been created.

The rm(<object>) function allows you to remove an object. When you exit the application, using q(), you can save the working environment so that you can continue later. The penultimate command is actually two commands on a single line. To separate two commands on one line, use a semicolon. In this case, the values of the variables x and y have been requested.

To get a result more similar to printf in C language, you can use the cat function, which must intersperse strings with variables.

You can redirect R's input and output with functions source(<file with commands>) and sink(<file with output>), but it's just as easy to insert the commands in a text file and simply copy/paste them into R so that the commands are executed.

The files are read/written from the directory in which R is run from.

 Action:
ação ícone

 Expected:
resultado ícone

> year <- 1345
> if(year%%4==0 && year%%100!=0 || year%%400==0) "Leap" else "Common"
 

In place of the equality operator for assignment, the <- operator was used. In R, this operator must be used for the assignment, and there is also the -> operator, meaning that the variable to be assigned is on the right, and the expression on the left.

Conditionals can be used in much the same way as in C. The division remainder operator is %%, and the integer division is %/%. The logical operators in C and R are the same.

In the R language, it doesn't make sense to know the size of variables, as it is a loosely typed language. In this type of language, the memory occupied by a variable or data structure should not be the programmer's concern. However, there are basic variable modes in R: numeric; complex; logical; character. To obtain the basic mode of a variable, use the mode(<object>) function.

 Action:
ação ícone

 Expected:
resultado ícone

> sum=0; i=1
> while(i<=4) { sum=sum+i*i; i=i+1 }
> sum
 

You can see a use of while, in a similar way to the C language. In this case, the first 4 square numbers are added together. You can use blocks of code via curly braces. An instruction can have several lines of code, but you can only edit each line at a time, so it's preferable to edit it in a file, copying it as soon as the code is ready.

 Action:
ação ícone

 Expected:
resultado ícone

> Leap <- function(year) year%%4==0 && year%%100!=0 || year%%400==0
> Leap(2344)
 

The definition of a function in R also consists of assigning an expression like:

function(<arguments>) expression

The expression can have a block of instructions, so that it can contain more than one command.
Once a function has been defined, it can be used inside other expressions.

 Action:
ação ícone

 Expected:
resultado ícone

> sum<-0
> for(i in 1:4) sum <- sum+i*i
> sum
 

The R language also allows for for loops, which are identical to while loops but with the iterator variable, just like the C language. Note, however, the notation "1:4". The variable i will have values from 1 to 4, and the expression will be evaluated for each of i's values.

 Action:
ação ícone

 Expected:
resultado ícone

> 1:4
> c(1,2,3,4)
 

These two expressions are the same and can be used to quickly build vectors with sequential content.

In R, there is also the repeat loop followed by an expression, which has no exit condition. The output of the cycle must use the "break" instruction, which exists in R with the same meaning as in C, and the "next" instruction is equivalent to "continue" in C. Usually, it is not advisable to use this type of instruction in either C or R, so the cycles to use should be the for and while loops.

 Action:
ação ícone

 Expected:
resultado ícone

> vector <- c(12, 45, 66, c(23, 455, 6))
> vector
> sum(vector)
> mean(vector)
 

In R you can easily define vectors. However, using a vector inside another vector adds the vector as an element (instead of appending its elements to the original vector). You can use loops for sums, averages, variances, etc., or simply use the functions already implemented for this purpose, without the need for loops, as in the example.

 Action:
ação ícone

 Expected:
resultado ícone

> vector[1]
> vector[vector>25]
> vector[vector>25] <- 0
> vector
 

To access an element of the vector, you can indicate the index (starting at 1), but you can also put in an expression based on the values in the vector, and thus define another vector whose elements satisfy the condition. The value is accessed with the name of the vector. You can even use this result to reset values, as shown in the last expression.

 Action:
ação ícone

 Expected:
resultado ícone

> matrix <- array(0, dim=c(10,10))
> matrix
> matrix[,1]
> matrix[1,]
 

The code above defines a 2-dimension array, 10 by 10, with its elements initialized at 0. Creating arrays can easily be done using the array function. The first argument could have a vector with initial values to place in the array, instead of 0. The second argument, "dim", has the dimensions of the array. You can define 2 and 3 dimensions, or more, but usually you don't need more than 3 dimensions. Accessing a position in the array is done by placing all the indices, separated by commas. In this case there is a difference from C.

The array visualization shows the column and row headings, and also reveals a simple notation for extracting part of the array. The index [,1] returns a vector with the first column of the array, while the index [1,] returns a vector with the first row of the array.