Professional Documents
Culture Documents
In-Built Functions in R
Numeric Functions
R Introduction 2
#To install package in R
install.packages(“package name”)
# To see all installed package in R
Installed.packages( )
# To activate the package
library(“name of packages”)
# To bring up help
help(command)
?command
#search for package
Search()
#to make package unavailable for use
Detach(package : name)
R Introduction 3
apropos(("plot")) #shows all commands that contain “plot”
[1] ".rs.api.savePlotAsImage" ".rs.replayNotebookPlots" "assocplot“
[4] "barplot" "barplot.default" "biplot"
[7] "boxplot" "boxplot.default" "boxplot.matrix"
[10] "boxplot.stats" "cdplot" "coplot"
[13] "fourfoldplot" "interaction.plot" "lag.plot"
[16] "matplot" "monthplot" "mosaicplot"
[19] "plot" "plot.default" "plot.design“
[22] "plot.ecdf" "plot.function" "plot.new"
[25] "plot.spec.coherency" "plot.spec.phase" "plot.stepfun"
[28] "plot.ts" "plot.window" "plot.xy"
[31] "preplot" "qqplot" "recordPlot"
[34] "replayPlot" "savePlot" "screeplot"
[37] "spineplot" "sunflowerplot" "termplot"
[40] "ts.plot"
R Introduction 4
Combining vectors Creating Contingency Tables
F<-c(2,3,5,7,6) table (f1)
F1<-c(F,8,9,3,2)
F1
Sorting and ranking the data Note that order() is different from rank().
sort(F1) rank() will return you rank of the elements
rank(F1) while order() returns the ranked element's
rank(sort(F1)) position in the original list:
cumsum(y)
[1] 2 NA NA NA
R Introduction 6
Descriptive Statistics
Descriptive Statistics
R provides a wide range of functions for obtaining summary statistics. One
method of obtaining descriptive statistics is to use the sapply( ) function
with a specified summary statistic. Possible functions used in sapply
include mean, sd, var, min, max, median, range, and quantile.
x<-c(2,3,4,6,NA)
Dealing with missing values
>sapply(x, mean, na.rm=TRUE)
[1] 2 3 4 6 NaN
R Introduction 8
Summary Command for Data Objects
x<-c(2,3,4,6,NA)
summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA‘s
2.00 2.75 3.50 3.75 4.50 6.00 1
Summary of Data Frames Apply command for summaries on Row or Columns
n<-data.frame(x=1:5,m1=c(2,3,4,3,2)) apply(n,1,mean)
summary(n) [1] 1.5 2.5 3.5 3.5 3.5
apply(n, 2,mean)
density(n) # used mostly for plot
x m1
max(n) / min(n) 3.0 2.8
length(n) sapply(n,mean,rm.na=TRUE)
rowMeans(n) x m1
colMeans(n) 3.0 2.8
rowSums(n)
R Introduction 9
Exercise: Create a data file of 20 subjects with age and income &
perform the descriptive analysis.
apply(), sapply(), tapply() in R
• The apply() function is the most basic of all collection. Along with this
sapply(), tapply() & lapply() are also used.
• The apply collection can be viewed as a substitute to the loop.
• They can be used for an input list, matrix or array and apply a
function.
• Any function can be passed into apply().
apply() function
apply() can be used for an input list, matrix or array and apply a
function. Any function can be passed into apply().
apply(X, MARGIN, FUN)
x: an array or matrix
MARGIN=1: the manipulation is performed on rows
MARGIN=2: the manipulation is performed on columns
MARGIN=c(1,2) the manipulation is performed on rows and columns
FUN: tells which function to apply. Built functions like mean, median, sum, min, max and even user-
defined functions can be applied.
apply ()
lapply() Function
lapply(X, FUN)
Arguments:-
X: A vector or an object
FUN: Function applied to each element of x
sapply(X, FUN)
Arguments:-
X: A vector or an object
FUN: Function applied to each element of x