Unit4 Advanced R Programming
Unit4 Advanced R Programming
Advanced R Programming
Math Functions
truc(x): truncate the value to the nearest number less than the number
> trunc(5.19)
[1] 5
round(x)
> round(3.475,digit=2)
[1] 3.48
substr: Extract or replaces substrings in a string
> substr("abcdef",2,4)
[1] "bcd“
sub function in R Language is used to replace the first match of a pattern in a string. If
there is a vector of string elements, then it will replace the first match of the pattern
from all elements.
Parameters:
pattern: string to be matched
replacement: string for replacement
string: String or String vector
ignore.case: Boolean value for case-sensitive replacement
x="Hello world hello“
> sub("ell","owe",x)
[1] "Howeo world hello“
> sub("hel","owe",x,ignore.case=FALSE)
[1] "Hello world owelo"
> sub("hel","owe",x,ignore.case=TRUE)
[1] "owelo world hello"
strsplit() Function in R
> paste("z",1:3,sep="")
[1] "z1" "z2" "z3"
> paste("z",1:3,sep="&Y")
[1] "z&Y1" "z&Y2" "z&Y3"
Str()
It is used to help you examine the structure of data object
x=read.csv(file.choose())
> str(x)
'data.frame':8 obs. of 5 variables:
$ id : int 1 2 3 4 5 6 7 8
$ name : chr "Rick" "Dan" "Michelle" "Ryan" ...
$ salary : num 623 515 611 729 843 ...
$ start_date: chr "2012-01-01" "2013-09-23" "2014-11-15" "2014-05-11" ...
$ dept : chr "IT" "Operations" "IT" "HR" ...
Summary()
It is designed to give a quick statistical summary of data objects
> summary(x)
id name salary
Min. :1.00 Length:8 Min. :515.2
1st Qu.:2.75 Class :character 1st Qu.:602.8
Median :4.50 Mode :character Median :628.0
Mean :4.50 Mean :656.9
3rd Qu.:6.25 3rd Qu.:724.1
Max. :8.00 Max. :843.2
start_date dept
Length:8 Length:8
Class :character Class :character
Mode :character Mode :character
toupper(x)- changes the letter of string to uppercase
Tolower(x)- changes the letter of string to lowercase
> toupper("aashima")
[1] "AASHIMA"
> tolower("AASHIMA")
[1] "aashima"
> tolower("Aashima")
[1] "aashima"
Apply Family of Functions
• Functions present in the apply family are the ones that allow us to
manipulate data frames, arrays, matrices, vectors. These functions
are alternative to the loops. However, are more efficient than loops
as functions are faster at the execution level. These functions
reduce the need for explicitly creating a loop in R. Following is the
list of functions that are a part of the apply family.
• The apply() function
• The lapply() function
• The sapply() function
• The tapply() function
• The mapply() function
Apply()
Lapply()
Sapply()
Tapply()
Mapply()