OpenSource For You

Programmin­g in R

R is a language and environmen­t for statistica­l computing and graphics. It is a simple and effective programmin­g language, which includes conditiona­ls and loops. Often called GNU S, it is used to statistica­lly explore datasets and to make many graphical d

-

Ris a highly powerful computer language, an environmen­t and integrated suite of software facilities. The functional­ity of R can be easily extended via packages. A typical R studio window might have four panes as depicted in Figure 1.

The user can type the commands in Pane 1 and press Ctrl + Enter in order to execute the entered command. The output will appear in Pane 2, i.e., the pane with the caption ‘Console’. Alternativ­ely, the user might type the command in the Console pane itself and obtain the result on pressing the Enter key.

This section dwells on the following: accepting input from the keyboard, generating sequences, and random numbers in R.

scan() can be used for obtaining numeric inputs from the keyboard, as shown below:

Now the user can enter the numbers in the Console window and press the Enter key instead of entering a number in order to indicate the end of data input. The numbers will be assigned to x2.

Sequence-generating operator

The colon (:) can be used to generate sequences in R, as follows:

For shuffling the values stored in x, the sample() function can be used:

The values stored in q might be: 13 14 16 19 17 15 12 11 18 20. The values stored in x remain intact.

seq() can be used in R for generating a sequence, as follows:

A sample output follows:

In Figure 1, the command from is for specifying the starting value of the sequence, to is for specifying the ending value of the sequence, and by is to increment the sequence.

The concatenat­ion function can be used to store values in x1, as shown below:

Random numbers in R

Random numbers are used in simulation and they are also used by statistici­ans.

To generate a random number (having a fractional part) between 11 and 15, and store it in rnum, the following command can be used:

By default, runif(n) generates random numbers in the range of 0 to 1.

In order to generate three integer random numbers between 11 and 15, the following command can be used:

11:15 is the range between which random numbers have to be generated, 3 the number of random numbers to be generated and replace = T, which indicates that repeats are permissibl­e.

For generating a sequence of random numbers, and to generate the same sequence later, set.seed() and runif() can be used as follows:

The section below dwells on vector and matrix operations in R.

Vectors in R

The R programmin­g environmen­t provides very powerful vector and matrix operation tools. The code below creates a vector for storing the first 13 members of the Fibonacci series:

The output of the above code would be:

fib <- numeric(13): Sets up a numeric vector the length of which is 13 and this vector is initialise­d with zeroes.

fib[1] <- 0: Sets the first element of vector fib to the value 0. The next statement is self-explanator­y.

The for() loop computes the third to the thirteenth element (except for the first two elements, each term in the Fibonacci series is the sum of the preceding two terms).

fib: Displays the elements of the vector fib.

The contents of the fib vector can also be printed with the help of the for() loop, as shown below:

A sample output is shown below:

is.vector(x) and is.matrix(x) can be used to determine whether x is a vector or matrix.

> is.vector(fib) will yield the following:

And > is.matrix(fib) will yield what follows:

A few more commands (along with the output) related to vector operations are as follows:

Matrix operations in R

This part of the article dwells on the terseness of complex matrix operations in R.

The command…

…creates a 3 x 3 matrix as follows:

The command…

…creates a 3 x 3 matrix as follows:

In order to display matrix M, the user can simply type the following command…

M

…at the ‘>’ prompt and press Ctrl + Enter. For adding matrices M and N, and storing the result in matrix A, the user can type the following commands:

The output will appear as follows:

In order to store the transpose of M into TM and for displaying TM, the following commands can be used:

To multiply matrices M and N and store the result in matrix MM, the following commands can be used:

If the commands typed are…

...then the output will be:

Figure 2: Output of M* N

To find the determinan­t of M and display the result, use the following commands:

To find the inverse of matrix M, use the following command:

To find the rank of matrix M, type in the following command:

A typical output is shown below:

For combining matrices M and N (columnwise), use the following command:

For combining matrices M and N (row wise), use the following command:

Pie charts in R

Assume that a csv file ( C:\Users\faculty\Documents\info.csv) contains the data given in Figure 3.

A pie chart can be drawn based on the data in Figure 3, with the following commands:

R automatica­lly assigns names to the rows and columns. The row names and column names can be displayed using the following commands:

To display the values of V1 and V2, the following commands can be used:

A pie chart will appear in the Plots window as shown in Figure 4. pie3D() can be used for generating a 3D pie chart. To use pie3D(), it is necessary to install the plotrix package:

R language can be used to perform highly complex operations related to statistics as well as econometri­cs. It can also be used for working on images and mathematic­al modelling. In fact, many premier institutio­ns in several countries have made R a part of their regular curriculum, and insist on researcher­s using this language for carrying out statistica­l research.

 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 1: A typical R studio window
Figure 1: A typical R studio window
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 4: Pie chart in the Plots window
Figure 4: Pie chart in the Plots window
 ??  ?? Figure 5: 3D pie chart
Figure 5: 3D pie chart
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India