Chapter 3
String Manipulations
InthepreviouschapterwetalkedabouthowRtreatsobjectswithcharacters,andhowwe
canimporttextdata. Nowwewillcoversomeofthebasic(andnotsobasic)functionsfor
manipulatingcharacterstrings.
3.1 The versatile paste() function
The functionpaste() is perhaps oneofthe most important functionsthatwecanuseto
createandbuildstrings.paste()takesoneormoreRobjects,convertsthemto"character",
andthenitconcatenates (pastes) themtoformone orseveralcharacter strings. Its s usage
hasthefollowingform:
paste(..., sep = " ", collapse = NULL)
The argument t ... means that t it takes any number of objects. The e argument sep is a
characterstringthatisusedasaseparator.Theargumentcollapseisanoptionalstringto
indicateifwewantallthetermstobecollapsedintoasinglestring.Hereisasimpleexample
withpaste():
# paste
PI = paste("The life of", pi)
PI
## [1] "The life of 3.14159265358979"
As you can see, , the e default separator is ablank k space (sep = " "). But t you can select
anothercharacter,forexamplesep = "-":
23
Change pdf to powerpoint on - software control dll:C# Create PDF from PowerPoint Library to convert pptx, ppt to PDF in C#.net, ASP.NET MVC, WinForms, WPF
Online C# Tutorial for Creating PDF from Microsoft PowerPoint Presentation
www.rasteredge.com
Change pdf to powerpoint on - software control dll:VB.NET Create PDF from PowerPoint Library to convert pptx, ppt to PDF in vb.net, ASP.NET MVC, WinForms, WPF
VB.NET Tutorial for Export PDF file from Microsoft Office PowerPoint
www.rasteredge.com
24
# paste
IloveR = paste("I""love""R", sep = "-")
IloveR
## [1] "I-love-R"
Ifwegivepaste()objectsofdierentlength,thenitwillapplyarecyclingrule.Forexample,
ifwepasteasinglecharacter "X" withthesequence 1:5,andseparator sep = "." " thisis
whatweget:
# paste with objects of different lengths
paste("X", 1:5, sep p = ".")
## [1] "X.1" "X.2" "X.3" "X.4" "X.5"
Toseetheeectofthecollapseargument,let’scomparethedierencewithcollapsingand
withoutit:
# paste with collapsing
paste(1:3, c("!""?""+"), sep = "", collapse e = "")
## [1] "1!2?3+"
# paste without collapsing
paste(1:3, c("!""?""+"), sep = "")
## [1] "1!" "2?" "3+"
One ofthe potentialproblems with paste() is that itcoerces missingvalues NA into the
character"NA":
# with missing values NA
evalue = paste("the value e of f  e  is"exp(1), NA)
evalue
## [1] "the value of  e  is 2.71828182845905 NA"
Inadditiontopaste(),there’salsothefunctionpaste0()whichistheequivalentof
paste(..., sep = "", collapse)
# collapsing with paste0
paste0("let s""collapse""all""these""words")
## [1] "let scollapseallthesewords"
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
software control dll:Online Convert PowerPoint to PDF file. Best free online export
Online Powerpoint to PDF Converter. Download Free Trial. Then just wait until the conversion from Powerpoint to PDF is complete and download the file.
www.rasteredge.com
software control dll:RasterEdge XDoc.PowerPoint for .NET - SDK for PowerPoint Document
Able to view and edit PowerPoint rapidly. Convert. Convert PowerPoint to PDF. Convert PowerPoint to HTML5. Convert PowerPoint to Tiff. Convert PowerPoint to Jpeg
www.rasteredge.com
25
3.2 Printing characters
Rprovidesaseriesoffunctionsforprintingstrings.Someoftheprintingfunctionsareuseful
whencreatingprintmethodsforprogrammedobjects’classes. Otherfunctionsareuseful
forprintingoutputseitherintheRconsoleorinagivenle.Inthissectionwewilldescribe
thefollowingprint-relatedfunctions:
Printingfunctions
Function
Description
print()
genericprinting
noquote()
printwithnoquotes
cat()
concatenation
format()
specialformats
toString() converttostring
sprintf()
printing
3.2.1 Printingvalueswithprint()
TheworkhorseprintingfunctioninRisprint().Asitsnamesindicates,thisfunctionprints
itsargumentontheRconsole:
# text string
my_string = "programming with data is s fun"
# print string
print(my_string)
## [1] "programming with data is fun"
To bemore precise,print() is ageneric function,whichmeans that youshouldusethis
functionwhencreatingprintingmethodsforprogrammedclasses.
Asyoucanseefromthepreviousexample,print()displaystextinquotedformbydefault.
Ifwewanttoprintcharacterstringswithnoquoteswecansettheargumentquote = FALSE
# print without quotes
print(my_string, quote e = FALSE)
## [1] programming with data is fun
Theoutputproducedbyprint()canbecustomizedwithvariousoptionalarguments.How-
ever, the e way in which print() displays objects is rather limited. To o get more printing
varietythereisanumberofmore exiblefunctionsthatwecanuse.
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
software control dll:C# WinForms Viewer: Load, View, Convert, Annotate and Edit
to PDF; Convert PowerPoint to PDF; Convert Image to PDF; Convert Jpeg to PDF; Merge PDF Files; Split PDF Document; Remove Password from PDF; Change PDF Permission
www.rasteredge.com
software control dll:How to C#: Overview of Using XDoc.PowerPoint
How to C#: Overview of Using XDoc.PowerPoint. Overview for How to Use XDoc.PowerPoint in C# .NET Programming Project. PowerPoint Conversion.
www.rasteredge.com
26
3.2.2 Unquotedcharacterswithnoquote()
Weknowthatwecanprinttextwithoutquotesusingprint()withitsargumentquote =
FALSE.Analternativeoptionforachievingasimilaroutputisbyusingnoquote(). Asits
namesimplies,thisfunctionprintscharacterstringswithnoquotes:
# noquote
noquote(my_string)
## [1] programming with data is fun
Tobemoreprecisenoquote()createsacharacterobjectofclass "noquote"whichalways
getsdisplayedwithoutquotes:
# class noquote
no_quotes = noquote(c("some""quoted""text""!%^(&="))
# display
no_quotes
## [1] some
quoted text
!%^(&=
# check class
class(no_quotes)
## [1] "noquote"
# test character
is.character(no_quotes)
## [1] TRUE
# no quotes even when subscripting
no_quotes[2:3]
## [1] quoted text
3.2.3 Concatenateandprintwith cat()
Anotherveryusefulfunctioniscat()whichallowsustoconcatenateobjectsandprintthem
eitheronscreenortoale. Itsusagehasthefollowingstructure:
cat(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)
Theargument...impliesthatcat()acceptsseveraltypesofdata. However,whenwepass
numericand/orcomplexelementstheyareautomaticallyconvertedtocharacterstringsby
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
software control dll:C# HTML5 Viewer: Load, View, Convert, Annotate and Edit PowerPoint
Such as load and view PowerPoint without Microsoft Office software installed, convert PowerPoint to PDF file, Tiff image and HTML file, as well as add
www.rasteredge.com
software control dll:VB.NET PowerPoint: Read, Edit and Process PPTX File
create image on desired PowerPoint slide, merge/split PowerPoint file, change the order of How to convert PowerPoint to PDF, render PowerPoint to SVG
www.rasteredge.com
27
cat(). Bydefault,thestringsareconcatenatedwithaspacecharacterasseparator. This
canbemodiedwiththesepargument.
Ifweusecat()withonlyonesinglestring,yougetasimilar(althoughnotidentical)result
asnoquote():
# simply print with  cat()
cat(my_string)
## programming with data is fun
Asyoucansee,cat()printsitsargumentswithoutquotes.Inessence,cat()simplydisplays
itscontent(onscreenorinale).Comparedtonoquote(),cat()doesnotprintthenumeric
lineindicator([1]inthiscase).
Theusefulnessofcat()iswhenwehavetwoormorestringsthatwewanttoconcatenate:
# concatenate and print
cat(my_string, "with R")
## programming with data is fun with R
Youcanusetheargumentseptoindicateachacractervectorthatwillbeincludedtoseparate
theconcatenatedelements:
# especifying  sep
cat(my_string, "with R", sep = " =) ) ")
## programming with data is fun =) with R
# another example
cat(1:10, sep = "-")
## 1-2-3-4-5-6-7-8-9-10
Whenwepassvectorstocat(),eachoftheelementsaretreatedasthoughtheywereseparate
arguments:
# first four months
cat(month.name[1:4], sep p = " ")
## January February March April
The argumentfill allows us tobreak long strings; ; this s is achieved when wespecify the
stringwidthwithanintegernumber:
# fill = 30
cat("Loooooooooong strings""can be e displayed""in a a nice format",
"by using the  fill  argument", fill l = = 30)
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
software control dll:VB.NET PDF Password Library: add, remove, edit PDF file password
Add password to PDF. Change PDF original password. Remove password from PDF. Set PDF security level. VB: Change and Update PDF Document Password.
www.rasteredge.com
software control dll:C# powerpoint - Convert PowerPoint to PDF in C#.NET
C# PowerPoint - Convert PowerPoint to PDF in C#.NET. Online C# Tutorial for Converting PowerPoint to PDF (.pdf) Document. PowerPoint to PDF Conversion Overview.
www.rasteredge.com
28
## Loooooooooong strings
## can be displayed
## in a nice format
## by using the  fill  argument
Lastbutnotleast,wecanspecifyaleoutputincat().Forinstance,let’ssupposethatwe
wanttosavetheoutputintheleoutput.txtlocatedinourworkingdirectory:
# cat with output in a given file
cat(my_string, "with R", file = "output.txt")
3.2.4 Encodingstringswithformat()
The function n format() allows us to format an R object for pretty printing. Essentially,
format()treatstheelementsofavectorascharacterstringsusingacommonformat. This
isespeciallyusefulwhenprintingnumbersandquantitiesunderdierentformats.
# default usage
format(13.7)
## [1] "13.7"
# another example
format(13.12345678)
## [1] "13.12"
Someusefularguments:
 widththe(minimum)widthofstringsproduced
 trimifsettoTRUEthereisnopaddingwithspaces
 justify controls how w padding takes place for strings. Takes the values "left",
"right", "centre", "none"
Forcontrolingtheprintingofnumbers,usethesearguments:
 digitsThenumberofdigitstotherightofthedecimalplace.
 scientificuseTRUEforscienticnotation,FALSEforstandardnotation
Keepinmindthatjustifydoesnotapplytonumericvalues.
# use of  nsmall
format(13.7, nsmall = = 3)
## [1] "13.700"
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
29
# use of  digits
format(c(6, 13.1), , digits s = = 2)
## [1] " 6" "13"
# use of  digits  and  nsmall
format(c(6, 13.1), , digits s = = 2, , nsmall = = 1)
## [1] " 6.0" "13.1"
Bydefault,format()padsthestringswithspacessothattheyareallthesamelength.
# justify options
format(c("A""BB""CCC"), width = 5, justify y = "centre")
## [1] " A " " BB " " CCC "
format(c("A""BB""CCC"), width = 5, justify y = "left")
## [1] "A
" "BB
" "CCC "
format(c("A""BB""CCC"), width = 5, justify y = "right")
## [1] "
A" "
BB" " CCC"
format(c("A""BB""CCC"), width = 5, justify y = "none")
## [1] "A"
"BB" "CCC"
# digits
format(1/1:5, digits s = = 2)
## [1] "1.00" "0.50" "0.33" "0.25" "0.20"
# use of  digits , widths and justify
format(format(1/1:5, digits s = 2), width = = 6, , justify y = "c")
## [1] " 1.00 " " 0.50 " " 0.33 " " 0.25 " " 0.20 "
Forprintinglargequantitieswithasequencedformatwecanusethearguments big.mark
orbig.interval.Forinstance,hereishowwecanprintanumberwithsequencesseparated
byacomma","
# big.mark
format(123456789, big.mark k = ",")
## [1] "123,456,789"
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
30
3.2.5 C-stylestringformattingwithsprintf()
Thefunctionsprintf()isawrapperfortheCfunctionsprintf()thatreturnsaformatted
string combining text and variable e values. The nice e feature about sprintf() is that t it
providesusavery exiblewayofformattingvectorelementsascharacterstrings.Itsusage
hasthefollowingform:
sprintf(fmt, ...)
Theargumentfmtisacharactervectorofformatstrings.Theallowedconversionspecica-
tionsstartthesymbol%followedbynumbersandletters. Fordemonstrationpurposeshere
areseveralwaysinwhichthenumberpicanbeformatted:
#  %f  indicates  fixed point  decimal notation
sprintf("%f", pi)
## [1] "3.141593"
# decimal notation with 3 decimal digits
sprintf("%.3f", pi)
## [1] "3.142"
# 1 integer and 0 decimal digits
sprintf("%1.0f", pi)
## [1] "3"
# decimal notation with 3 decimal digits
sprintf("%5.1f", pi)
## [1] " 3.1"
sprintf("%05.1f", pi)
## [1] "003.1"
# print with sign (positive)
sprintf("%+f", pi)
## [1] "+3.141593"
# prefix a space
sprintf("% f", pi)
## [1] " 3.141593"
# left adjustment
sprintf("%-10f", pi) ) # # left justified
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
31
## [1] "3.141593 "
# exponential decimal notation  e
sprintf("%e", pi)
## [1] "3.141593e+00"
# exponential decimal notation  E
sprintf("%E", pi)
## [1] "3.141593E+00"
# number of significant digits (6 by default)
sprintf("%g", pi)
## [1] "3.14159"
3.2.6 Convertingobjectstostringswith toString()
ThefunctiontoString()allowsustoconvertanRobjecttoacharacterstring.Thisfunction
canbeusedasahelperforformat()toproduceasinglecharacterstringfromseveralobejcts
insideavector.Theresultwillbeacharactervectoroflength1withelementsseparatedby
commas:
# default usage
toString(17.04)
## [1] "17.04"
# combining two objects
toString(c(17.04, 1978))
## [1] "17.04, 1978"
# combining several objects
toString(c("Bonjour", 123, TRUE, NA, log(exp(1))))
## [1] "Bonjour, 123, TRUE, NA, 1"
OneofthenicefeaturesabouttoString()isthatyoucanspecifyitsargumentwidthtox
amaximumeldwidth.
# use of  width
toString(c("one""two""3333333333"), width = = 8)
## [1] "one,...."
# use of  width
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
32
toString(c("one""two""3333333333"), width = = 12)
## [1] "one, two...."
3.2.7 Comparingprintingmethods
EventhoughRhasjustasmallcollectionoffunctionsforprintingandformattingstrings,
wecanusethemtogetawidevarietyofoutputs.Thechoiceoffunction(anditsarguments)
willdependonwhat wewanttoprint,howwewanttoprintit,andwhere wewanttoprint
it. Sometimestheanswerofwhichfunctiontouseisstraightforward. Sometimeshowever,
wewouldneedtoexperimentandcomparedierentways untilwendthemostadequate
method. Tonishthissectionlet’sconsiderasimpleexamplewithanumericvectorwith5
elements:
# printing method
print(1:5)
## [1] 1 2 3 4 5
# convert to character
as.character(1:5)
## [1] "1" "2" "3" "4" "5"
# concatenation
cat(1:5, sep = "-")
## 1-2-3-4-5
# default pasting
paste(1:5)
## [1] "1" "2" "3" "4" "5"
# paste with collapsing
paste(1:5, collapse e = "")
## [1] "12345"
# convert to a single string
toString(1:5)
## [1] "1, 2, 3, 4, 5"
# unquoted output
noquote(as.character(1:5))
CCBY-NC-SA3.0 GastonSanchez
HandlingandProcessingStringsinR
Documents you may be interested
Documents you may be interested