0% found this document useful (0 votes)
3 views

Unit4 Advanced R Programming

Uploaded by

Osaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit4 Advanced R Programming

Uploaded by

Osaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Unit-4

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“

nchar-no. of character in a string


> str <- "Big Data"
> nchar(str)
[1] 8
Find position of a Matched Pattern in a String in R
Programming – grep() Function

grep() function in R Language is used to search for matches of


a pattern within each element of the given string.
Syntax:
grep(pattern, x, ignore.case=TRUE/FALSE, value=TRUE/FALSE)
Parameters:
pattern: Specified pattern which is going to be matched with
given elements of the string.
x: Specified string vector.
ignore.case: If its value is TRUE, it ignores case.
value: If its value is TRUE, it return the matching elements
vector, else return the indices vector.
> x <- c("GFG", "gfg", "xyz", "XYZ")
> # Calling grep() function
> grep("gfg", x)
[1] 2
> grep("xyz", x)
[1] 3
> grep("gfg", x, ignore.case = FALSE)
[1] 2
> grep("xyz", x, ignore.case = TRUE)
[1] 3 4
> x <- c("GFG", "gfg", "xyz", "Xyz")
> grep("xyz", x, ignore.case = TRUE)
[1] 3 4
Replace the First Match of a Pattern from a String in R
Programming – sub() Function

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.

Syntax: sub(pattern, replacement, string, ignore.case=TRUE/FALSE)

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

The strsplit() in R programming language function is used to split the


elements of the specified character vector into substrings according to
the given substring taken as its parameter.
> strsplit("xyz","")
[[1]]
[1] "x" "y" "z"
paste(…,sep=“”)
Joins strings after using the sep string to separate them
> strsplit("xyz","")
[[1]]
[1] "x" "y" "z"

> 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()

You might also like