Professional Documents
Culture Documents
INTRODUCTION
INTRODUCTION
UNIT I - INTRODUCTION
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
- The interactive mode is normally used which executes the command and outputs the result without the the
> 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
● 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
● length(p) gives 3
● Any data can be taken by using data() and the mean and standard deviation can be found.
vectors in R
Check following code
> y <- vector(length=2)
> y[1]<-1
> y[2]<-3
>y
[1] 1 3
Functions
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
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
2. Siegel, S. (1956), Nonparametric Statistics for the Behavioral Sciences, McGraw-Hill International,
Auckland.