Data Visualization and Exploration with R
String Functions
1
2081 CSE
Functions
String Functions
1. cat
2. [Link]
3. charmatch
4. charToRaw
5. chartr
6. sQuote,dQuote
7. encodeString
8. grep, grepl, sub, gsub, regexpr, gregexpr, regexec
9. nchar, nzchar
Y
10. length
11. noquote
T
12. paste, paste0
13. strsplit
SI
14. strwrap
15. replace
16. substr
ER
17. strtoi
18. toString
19. sprintf
IV
N
U
M
A
IT
G
Dr. Srikanth Thota@CSE 2
String Functions 2081 CSE
• cat
• Concatenate and Print
• cat(. . . , file=“ “,sep =” “,fill = FALSE,labels = NULL,append = FALSE)
cat("Hello","\n")
## Hello
cat("He secured"," 9 CGPA","\n")
## He secured 9 CGPA
m=1;n=2;o=3
cat(m,n,o,sep=",")
## 1,2,3
• [Link]
TY
• Expand a String with Respect to a Target Table
• [Link](input, target, nomatch = stop(“no match”))
[Link] <- c("Flight", "Foot", "Foot-Note","Feet","Foot-Hill")
SI
[Link]("Foot", [Link], nomatch = stop("no match"))
## [1] "Foot"
ER
[Link]("Foo", [Link],nomatch = stop("no match"))
## character(0)
IV
[Link]("Flu", [Link],warning("no match"))
UN
## Warning in eval(nomatch): no match
## [1] NA
• charmatch
• Partial String Matching
M
• charmatch(x, table, nomatch = NA_integer_)
TA
[Link] <- c("Flight", "Foot", "Foot-Note","Feet","Foot-Hill")
charmatch("F",[Link])
GI
## [1] 0
charmatch("Foot",[Link])
## [1] 2
charmatch("Foot-",[Link])
## [1] 0
• charToRaw
• Convert to or from Raw Vectors
• charToRaw(x)
sr=charToRaw("Hello")
sr
## [1] 48 65 6c 6c 6f
rawToChar(sr, multiple = FALSE)
Dr. Srikanth Thota@CSE 3
String Functions 2081 CSE
## [1] "Hello"
rawToChar(sr, multiple = TRUE)
## [1] "H" "e" "l" "l" "o"
• charToRaw
• Convert to or from Raw Vectors
• charToRaw(x)
sr=charToRaw("Hello")
rawShift(sr, -1)
## [1] 24 32 36 36 37
Y
rawToChar(rawShift(sr, -1), multiple = FALSE)
T
## [1] "$2667"
• chartr
SI
• Character Translation and Casefolding
• chartr(old, new, x)
ER
x<-"WALK"
chartr("W","T",x)
## [1] "TALK"
IV
tolower("WALK")
## [1] "walk"
N
toupper("talk")
## [1] "TALK"
U
• chartr
• Character Translation and Casefolding
M
• chartr(old, new, x)
x<-"WALK"
A
chartr("W","T",x)
## [1] "TALK"
IT
tolower("WALK")
## [1] "walk"
G
toupper("talk")
## [1] "TALK"
• sQuote
• Quote Text
• sQuote(x, q = getOption(“useFancyQuotes”))
• dQuote(x, q = getOption(“useFancyQuotes”))
options(useFancyQuotes = FALSE)
paste("argument", sQuote("x"), "should not be empty")
## [1] "argument 'x' should not be empty"
Dr. Srikanth Thota@CSE 4
String Functions 2081 CSE
options(useFancyQuotes = TRUE)
paste("argument", sQuote("x"), "should not be empty")
## [1] "argument ‘x’ should not be empty"
• sQuote
• Quote Text
• sQuote(x, q = getOption(“useFancyQuotes”))
• dQuote(x, q = getOption(“useFancyQuotes”))
options(useFancyQuotes = FALSE)
cat("\ndifference between ", sQuote("single"), "and",dQuote("double"), "quotes\n")
Y
##
## difference between 'single' and "double" quotes
T
#difference between 'single' and "double" quotes
options(useFancyQuotes = TRUE)
SI
cat("\ndifference between ", sQuote("single"), "and",dQuote("double"), "quotes\n")
##
ER
## difference between ‘single’ and “double” quotes
#difference between ‘single’ and “double” quotes
• encodeString
IV
• Encode Character Vector as for Printing
• encodeString(x, width = 0, quote = “ “, [Link] = TRUE,justify = c(”left”, “right”,
“centre”, “none”))
N
x <- c("\na", "ab", "abcde")
encodeString(x,1)
U
## [1] "\\na" "ab" "abcde"
x <- c("a", "ab", "abcde")
M
encodeString(x,3)
## [1] "a " "ab " "abcde"
A
• grep
IT
• Pattern Matching and Replacement
• grep(pattern, x, [Link] = FALSE, perl = FALSE, value = FALSE,fixed = FALSE,
useBytes = FALSE, invert = FALSE)
G
lg<-c("Java", "Python", "ADA", "COBOL")
lg
## [1] "Java" "Python" "ADA" "COBOL"
grep("A+", lg)
## [1] 3
grep("A+", lg,value = TRUE)
## [1] "ADA"
• grep
• Pattern Matching and Replacement
Dr. Srikanth Thota@CSE 5
String Functions 2081 CSE
• grep(pattern, x, [Link] = FALSE, perl = FALSE, value = FALSE,fixed = FALSE,
useBytes = FALSE, invert = FALSE)
txt <- c("feet","foot","football", "foot-hill")
grep("foo", txt)
## [1] 2 3 4
grep("foo", txt,value=TRUE)
## [1] "foot" "football" "foot-hill"
• grepl
• Pattern Matching and Replacement
Y
• grepl(pattern, x, [Link] = FALSE, perl = FALSE,fixed = FALSE, useBytes = FALSE)
lg<-c("Java", "Python", "ADA", "COBOL")
T
lg
SI
## [1] "Java" "Python" "ADA" "COBOL"
grepl("a+", lg)
ER
## [1] TRUE FALSE FALSE FALSE
grepl("A+", lg)
## [1] FALSE FALSE TRUE FALSE
IV
• sub
• Pattern Matching and Replacement
N
• sub(pattern, replacement, x, [Link] = FALSE, perl = FALSE,fixed = FALSE, useBytes
= FALSE)
U
lg<-c("Jack", "Jill-Jill")
lg
M
## [1] "Jack" "Jill-Jill"
sub("J", "M",lg)
A
## [1] "Mack" "Mill-Jill"
IT
gsub("J", "M",lg)
## [1] "Mack" "Mill-Mill"
G
• regexpr
• Pattern Matching and Replacement
• regexpr(pattern, text, [Link] = FALSE, perl = FALSE,fixed = FALSE, useBytes =
FALSE)
regexpr("me", c("mean","median","mode","meme"))
## [1] 1 1 -1 1
## attr(,"[Link]")
## [1] 2 2 -1 2
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
• gregexpr
Dr. Srikanth Thota@CSE 6
String Functions 2081 CSE
• Pattern Matching and Replacement
• gregexpr(pattern, text, [Link] = FALSE, perl = FALSE,fixed = FALSE, useBytes =
FALSE)
gregexpr("me", c("mean","median","mode","meme"))
## [[1]]
## [1] 1
## attr(,"[Link]")
## [1] 2
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
##
## [[2]]
TY
## [1] 1
## attr(,"[Link]")
## [1] 2
SI
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
##
##
##
[1] TRUE
[[3]]
ER
IV
## [1] -1
## attr(,"[Link]")
## [1] -1
UN
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
##
M
## [[4]]
TA
## [1] 1 3
## attr(,"[Link]")
## [1] 2 2
GI
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
• regexec
• Pattern Matching and Replacement
• regexec(pattern, text, [Link] = FALSE, perl = FALSE,fixed = FALSE, useBytes =
FALSE)
regexec("me", c("mean","median","mode","meme"))
## [[1]]
## [1] 1
## attr(,"[Link]")
## [1] 2
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
Dr. Srikanth Thota@CSE 7
String Functions 2081 CSE
## [1] TRUE
##
## [[2]]
## [1] 1
## attr(,"[Link]")
## [1] 2
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
##
## [[3]]
Y
## [1] -1
## attr(,"[Link]")
T
## [1] -1
## attr(,"[Link]")
SI
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
ER
##
## [[4]]
## [1] 1
## attr(,"[Link]")
IV
## [1] 2
## attr(,"[Link]")
## [1] "chars"
## attr(,"useBytes")
N
## [1] TRUE
U
• nchar,length
• Count the Number of Characters (or Bytes or Width)
• nchar(x, type = “chars”, allowNA = FALSE, keepNA = NA)
M
• nzchar(x, keepNA = FALSE)
s<- "R Programming Lab"
nchar(s)
A
## [1] 17
IT
nzchar(s)
## [1] TRUE
G
nzchar("")
## [1] FALSE
• length
• Length of an Object
• length(x)
s<- "R Programming Lab"
length(s)
## [1] 1
• noquote
• Class for ‘no quote’ Printing of Character Strings
• noquote(obj, right = FALSE)
Dr. Srikanth Thota@CSE 8
String Functions 2081 CSE
s<- "R Programming Language Lab"
s
## [1] "R Programming Language Lab"
noquote(s)
## [1] R Programming Language Lab
• paste
• Concatenate vectors after converting to characters
• paste (. . . , sep = ” “, collapse = NULL, recycle0 = FALSE) #### paste0(. . . , collapse =
NULL, recycle0 = FALSE)
Y
fle="[Link]"
paste("c:/Work/",fle)
T
## [1] "c:/Work/ [Link]"
SI
paste0("c:/Work/",fle)
## [1] "c:/Work/[Link]"
ER
• strsplit
• Split the Elements of a Character Vector
• strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
IV
str = "Hadoop Spark and Flink"
strsplit(str, " ")
## [[1]]
N
## [1] "Hadoop" "Spark" "and" "Flink"
U
• strwrap
• Wrap Character Strings to Format Paragraphs
• strwrap(x, width = 0.9 * getOption(“width”), indent = 0,exdent = 0, prefix = “ “, simplify
M
= TRUE, initial = prefix)
x = 'This is a very long title wrapped on multiple lines without the need to adjust it by hand'
strwrap(x, width=50)
A
## [1] "This is a very long title wrapped on multiple"
IT
## [2] "lines without the need to adjust it by hand"
cat("\n")
G
strwrap(x, width=20)
## [1] "This is a very long" "title wrapped on" "multiple lines"
## [4] "without the need to" "adjust it by hand"
• strwrap
• Wrap Character Strings to Format Paragraphs
• strwrap(x, width = 0.9 * getOption(“width”), indent = 0,exdent = 0, prefix = “ “, simplify
= TRUE, initial = prefix)
x = 'This is a very long title wrapped on multiple lines without the need to adjust it by hand'
strwrap(x,width = 20,indent = 5)
## [1] " This is a very" "long title wrapped" "on multiple lines"
## [4] "without the need to" "adjust it by hand"
Dr. Srikanth Thota@CSE 9
String Functions 2081 CSE
cat("\n")
strwrap(x,width = 20,exdent = 5)
## [1] "This is a very long" " title wrapped" " on multiple"
## [4] " lines without" " the need to" " adjust it by"
## [7] " hand"
• replace
• Replace Values in a Vector
• replace(x, list, values)
str = c("Spark","Dark","Mark","Flink")
replace(str,list=c(4),values='Blink')
## [1] "Spark" "Dark" "Mark" "Blink"
TY
• substr
• Extract or replace substrings in a character vector
• substr(x, start, stop)
SI
s <- "R Programming Language Lab"
substr(s,0,9)
## [1] "R Program"
• strtoi ER
IV
• Convert Strings to Integers
• strtoi(x, base = 0L)
s <- "48"
UN
strtoi(s)
## [1] 48
strtoi(s)*2
M
## [1] 96
TA
• toString
• Convert an R Object to a Character String
• toString(x, . . . )
GI
x <- c("a", "b", "aaaaaaaa")
x
## [1] "a" "b" "aaaaaaaa"
toString(x)
## [1] "a, b, aaaaaaaa"
toString(x, width = 8)
## [1] "a, b...."
• toString
• Convert an R Object to a Character String
• toString(x, . . . )
x <- c("a", "b", "aaaa")
toString(x, width = 8)
Dr. Srikanth Thota@CSE 10
String Functions 2081 CSE
## [1] "a, b...."
x <- c("a", "b", "aa")
toString(x, width = 8)
## [1] "a, b, aa"
• sprintf
• Use C-style String Formatting Commands
• sprintf(fmt, . . . )
• fmt - a character vector of format strings, each of up to 8192 bytes
sprintf("%s has got %.2f CGPA", "Alex", 9.5)
Y
## [1] "Alex has got 9.50 CGPA"
T
SI
ER
IV
N
U
M
A
IT
G
Dr. Srikanth Thota@CSE 11