33 R Programming – 2

Mr. Hardik Joshi

epgp books

 

Writing Scripts

 

In the previous module of R programming – 1, we had studied the capabilities provide by R Environment. We had seen the use of variables, operators, data structures and datasets in R. In this module, we will study how to write programs in R. This module will cover the basic programming constructs provided by R environment. Let us begin our study by typing a simple program that performs addition calculation. The following code performs addition of subject marks of 3 different subjects. Further, it calculates the percentage and is displayed on screen.

 

Rather than typing the program in a text editor and executing it from command prompt, we can use GUI to make the tasks easier. RStudio is a free GUI available for R programming. The following is a screen shot of RStudio:

Figure 1: Rstudio – GUI for R programming

 

As you can see in the GUI, there are 4 panels, there is an editor to type the programs, the output of which is seen in console at the bottom of editor. There is also a watch windows to observe the values of variables while the programming is running. The panel provided in bottom right corner lists files of the directory, packages installed in R, it can also be used to view plots or to browse help.

 

Decision Making

 

As in other programming languages, R supports decision making facilities like if statements.

The syntax of if statement is as below:

We must be careful while using if statement, since the closing parenthesis ‘}’ and else keyword must be in the same line. Let us see a sample program that uses decision making. The following example checks whether a number is negative or positive number and display accordingly:

Looping in R

 

R supports looping construct in the form of while loop and for loops. The syntax of while loop is as shown below:

The below code snippet display numbers from 1 to 10 using while loop. In the example, we have used the variable ‘i’ as looping variable.

The for loop iterates for definite number of values given in sequence. In the above example, we created a vector ‘x’ of values 1 to 8. In the above example, for loop iterated 8 times taking the assigned values one at a time. It must be noted that the for loop of R is different from that of C programming.

 

Break and Next keywords

 

The break and next keywords are used in R that are similar in other programming languages. The break keyword is used to come out of loop based on certain criteria while the next keyword is used to continue execution till the criteria is met. Let us see examples that demonstrate break and next keywords. In the below code, the loop must iterate for 15 times, however, when the loop encounter value of 13, it breaks out of the loop and terminates.

In the following example, the loop iterates for 20 times, however, when the loop encounters the value of 13, it skips the value (does not print) and continues till the terminating criteria is satisfied.

Writing Interactive Programs

 

Now, let us see an interactive program where the input is given by a user. The following program checks whether a number entered by the user is prime or not. In the first line of the program, you may notice that we accept input using ‘prompt()’ function, the input is read using the ‘readline()’ function and later converted into integer using the’as.integer()’ function.

 

In the following program, the for loop is used differently. We have iterated the loop from value 2 upto the value ‘num-1’. You may notice the syntax of for loop since it is bit different from the loop that we studied earlier in this module.

 

Reading data from files

 

Let us study how to ready data from CSV files. Suppose we have a file “input.csv” in the default working directory. We need to use the function “read.csv()” to read the data from CSV file. Further, we store the data into data frame. Each column of the data can be accessed by using ‘$’ sign. For instance, the field with heading “salary” can be accessed by using the format (data$salary)

 

Plotting in R

 

R supports ploting features, such that data can be plotted from files or from data frames. R supports variety of plotting formats. The most prominent ones are as below:

  • Box plots
  • Scatter Plots
  • Line Graphs
  • Pie charts
  • Bar charts
  • Histograms

Let  us see  how plots can  be created by issuing commands.  By issuing the “boxplot()”function, R creates a box plot as shown in following figure:

In the above command, we are plotting data from “mtcars” dataset. The x-axis and y-axis are labelled accordingly. We have plotted the values of columns “mpg” and “cyl” .

 

Figure 2: Boxplot generated by R

 

Let us see one more example where we create a scatter plot using the “mtcars” dataset. In the following example, we create a scatter plot using the “plot()” function of R. We have also generated a file in png format and the output will be created as a png file.

 

Figure 3: Scatter plot of Weight Vs. Milage

 

Functions in R

 

R supports creation of user-defined functions. The R functions can take arguments and return the arguments. A function is created using “function()” keyword. Syntax of creating function is as shown below:

Let us study an example where we create a function to print the square of numbers for a sequence of values. Let us create a user-defined function, “myfunction()” which calculates the square of values. In the following code snippet, you can see that we are creating a body of function. The bottom line issues a function call by specifying the function name and arguments passed onto that function.

 

Let us study one more example where we have created a “check()” function. This function is used to check whether we have passed a positive value, negative value or zero as an argument. The below code demonstrates the use of “check()” function

Let us summarize the key concepts covered in this module

  • Simple Programming in R
  • Using Loops and Decision statements
  • Using Graphics
  • Creating User Defined Functions
you can view video on R Programming – 2

References for R Programming:

  1. Felix Alvaro. R : Easy R programming for beginners, your step-by-step guide to learning R programming,
  2. John Braun and Duncan James Murdoch. A first course in statistical programming with R,
  3. John M. Chambers. 2008. Software for data analysis : programming with R, Springer.
  4. Michael J. Crawley. The R book,
  5. Tilman M. Davies. The book of R : a first course in programming and statistics,
  6. Mark. Gardener. 2012. Beginning R : the Statistical Programming Language., John Wiley & Sons.
  7. Colin (Colin Stevenson) Gillespie and Robin Lovelace. Efficient R programming : a practical guide to smarter programming,
  8. Garrett Grolemund. Hands-on programming with R,
  9. Thomas Mailund. Advanced Object-Oriented Programming in R : Statistical Programming for Data Science, Analysis and Finance,
  10. Norman S. Matloff. 2011. The art of R programming : tour of statistical software design, No Starch Press.
  11. Shuichi Ohsaki, Jori Ruppert-Felsot, and Daisuke Yoshikawa. R programming and its applications in financial mathematics,
  12. Kun Ren. Modern R programming cookbook : recipes to simplify your statistical applications,
  13. Kun Ren. Learning R programming : become an efficient data scientist with R,
  14. Omar. Trejo and Peter. C. Figliozzi. 2017. R Programming By Example Practical, hands-on projects to help you get started with R., Packt Publishing.
  15. Alain F. Zuur, Elena N. Ieno, and Erik. Meesters. 2009. A beginner’s guide to R, Springer.
  16. Anon. R: The R Project for Statistical Computing. Retrieved March 19, 2017 from https://www.r-project.org/