You are on page 1of 17

20ECPW301

R PROGRAMMING WITH LABORATORY

UNIT I - INTRODUCTION

1.1 - Introduction, How to run R, R Sessions, and Functions

II III

20ECPW301
R PROGRAMMING WITH LABORATORY
20ECPW301
R PROGRAMMING WITH LABORATORY

R Language
● R is a scripting language for statistical data manipulation and analysis. Released in 2014 for statisticians
and data miners.
● You can download R from http://r-project.org R is available for Linux, Mac, and Windows.
● There are about 25 packages supplied with R (called “standard” and “recommended” packages) and
many more are available through the CRAN family of Internet sites (via https://CRAN.R-project.org)-
Comprehensive R Archive Network
Features of R includes the following
● Available for Windows, Macs, Linux
● R/S are the de facto standard among professional statisticians
● Superior comparable in the commercial products’
● Apart from the statistical operation we can automate the analysis and create new functions.
20ECPW301
R PROGRAMMING WITH LABORATORY

Features of R contd...
● Object-oriented and functional programming structure
● Auto save of data
● Open software and hence can be upgraded by the user community with new functions.
● Nominal execution speed.
● Less debugging as we have less code.
● Easier transition to parallel programming.
● R is Object oriented - performance of statistical regression analysis returns object containing - —estimated
coefficients, their standard errors, residuals etc while SAS(software analysis solution) and SPSS(statistical
package for social science) given bulk of outputs.
● R is polymorphic where same function(generic functions) can be applied to different types of objects. Eg plot()
function
● R can combine several commands with output of one being input to other
20ECPW301
R PROGRAMMING WITH LABORATORY

COMPARISON WITH OTHER LANGUAGES


20ECPW301
R PROGRAMMING WITH LABORATORY

Getting started with R


R studio can be downloaded from the following link https://www.rstudio.com/products/rstudio/older-versions/
Two modes of R are
● Interactive mode
● Batch mode
Interactive mode

- The interactive mode is normally used which executes the command and outputs the result without the the

need for print() command

> source(“name.R”)

- A function written can as well be taken up by the R interpreter and can be stored in the memory which when

called can be executed


20ECPW301
R PROGRAMMING WITH LABORATORY

Getting started with R


Batch Mode

● One way to run R in batch mode is from the system command line (not the R console).
● By running R from the system command line, it’s possible to run a set of commands without starting R.
● This makes it easier to automate analyses.
● Batch mode automatically captures warnings and errors and also it automatically invokes PROC.time() to
know how long it took for execution.
● options to suppresses the prompts, commands with only the output is also available in R
20ECPW301
R PROGRAMMING WITH LABORATORY

Working with R
● Download R studio
● To work in the interactive mode check the work directory use getwd()
● To set work directory use setwd()
● To work in the script you can go to file -select new R script and type the code and save in .r format
● To run the program just press cntrl+enter or click run icon at the right corner of the screen
● Use cntrl L to clear the console
● To quit the console use q() command
● The assignment operators <- , ->, = assign() function can be used in R
● Typeof() command gives the type of the data used in R which returns double,complex,character..
20ECPW301
R PROGRAMMING WITH LABORATORY

R- STUDIO VIEW
20ECPW301
R PROGRAMMING WITH LABORATORY

vectors in R

● p <- c(3,4,5) generates a 3, 1 element vector. ‘C’ stands for concatenation

● length(p) gives 3

● q <- c(p,1,2) gives the output q = 3,4,5,1,2

● q[3] gives the output 5

● mean(q) gives the mean of elements in q

● sd(q) - gives the standard deviation of the elements in the vector q

● Any data can be taken by using data() and the mean and standard deviation can be found.

● For eg hist(Nile) gives the following output


20ECPW301
R PROGRAMMING WITH LABORATORY

vectors in R
Check following code
> y <- vector(length=2)
> y[1]<-1
> y[2]<-3
>y
[1] 1 3

What does the following code


> y <- vector(length=2)
> y[1]<-1
>y
---------------
20ECPW301
R PROGRAMMING WITH LABORATORY

Functions

Functions can be written as


● Name of the function <- function(x)
○ Eg: oddcount <- function (x)
● Function can be called as function name(arguments)
○ Eg: oddcount(c(3.4.5.6.7))
○ This calls the function odd count and yield the result of number of odd counts in the vector given.
● Write a function to find the number of odd values in vec1=(1,2,3,4,5,6) and vec2(7,5,4,6,3,9) simultaneously.
20ECPW301
R PROGRAMMING WITH LABORATORY

Solution
● Vec1 <- c(1,2,3,4)
● Vec2 <- c(3,4,5,6,7,8,9)
● list1 <- c(vec1,vec2)
● > list1
● [1] 1 2 3 4 3 4 5 6 7 8 9
● > oddcount(list1)
● [1] 6
20ECPW301
R PROGRAMMING WITH LABORATORY

Super assignment
● Since the arguments in the function of R is read only.
● So any change in the local variable can be done by reassigning the return value of the function.
● For eg: w<-3
sumone<-function(x)
x<-x+1
w<- sumone(w)

The above code will not return value of 4 for ‘w’ as the return value is not used
Hence we use
w<-3
sumone<-function(x)
x<-x+1
return(x)
w<- sumone(w)
20ECPW301
R PROGRAMMING WITH LABORATORY

Super assignment
>w<-0
>addone<-function() w<<-w+1
> addone()
>w
[1] 1

Here <<- is the superassignment operator used in R to use the global variable from within the function
20ECPW301
R PROGRAMMING WITH LABORATORY

Default Arguments

In any function definition we can use the form

function(x,y=2)

Where if not assigned the value of y is taken to be 2 if the programmer does not assign value in function call
sum <- function (x,y=9)
z=x+y
> return z
w<-sum(2)
>w
[1] 11
20ECPW301
R PROGRAMMING WITH LABORATORY

Matrices
● A vector with two attributes - ‘Row’ and ‘Column ‘is called the matrix
● Matrix is given as the combination of two vectors using the command ‘rbind’
● q<- rbind(c(2,1),c(3,4)) and this gives the result
2 1
3 4
● Now multiplication of q with another vector can be done as q %*% c(2,2)
This gives the result
2 1 4+2 6
3 4 *2 2 = 6+8 = 14

● Write the R code to multiply two 3X3 matrices and verify the result
20ECPW301
R PROGRAMMING WITH LABORATORY

References

1. The Art of R Programming, Norman Matloff, Cengage Learning.

2. Siegel, S. (1956), Nonparametric Statistics for the Behavioral Sciences, McGraw-Hill International,
Auckland.

● R Programming Examples - DataMentor


● https://coursera.org/share/38a5862a6c7d8720804f8152140af327

You might also like