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

How To Trim Leading and Trailing Whitespace in R - Stack Overflow

This document discusses various ways to trim leading and trailing whitespace in R. 1. The trim(), trim.leading(), and trim.trailing() functions can be used to remove whitespace from strings. 2. The str_trim() function from the stringr package also removes whitespace from strings. 3. The grep(), grepl(), and sub() functions can be used to find and remove whitespace from vector elements.

Uploaded by

Jeiel França
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

How To Trim Leading and Trailing Whitespace in R - Stack Overflow

This document discusses various ways to trim leading and trailing whitespace in R. 1. The trim(), trim.leading(), and trim.trailing() functions can be used to remove whitespace from strings. 2. The str_trim() function from the stringr package also removes whitespace from strings. 3. The grep(), grepl(), and sub() functions can be used to find and remove whitespace from vector elements.

Uploaded by

Jeiel França
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

signup

login

StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,no
registrationrequired.

tour

HowtotrimleadingandtrailingwhitespaceinR?StackOverflow

27/12/2014

help

stackoverflowcareers

Takethe2minutetour

How to trim leading and trailing whitespace in R?

Iamhavingsometroubleswithleadingandtrailingwhitespaceinadata.frame.EgIliketotakealookat
aspecific row ina data.frame basedonacertaincondition:
>myDummy[myDummy$country==c("Austria"),c(1,2,3:7,19)]
[1]codeHelpercountrydummyLIdummyLMIdummyUMI
[6]dummyHInonOECDdummyHIOECDdummyOECD
<0rows>(or0lengthrow.names)
IwaswonderingwhyIdidn'tgettheexpectedoutputsincethecountryAustriaobviouslyexistedinmy
data.frame .AfterlookingthroughmycodehistoryandtryingtofigureoutwhatwentwrongItried:
>myDummy[myDummy$country==c("Austria"),c(1,2,3:7,19)]
codeHelpercountrydummyLIdummyLMIdummyUMIdummyHInonOECDdummyHIOECD
18AUTAustria00001
dummyOECD
181
AllIhavechangedinthecommandisanadditionalwhitespaceafterAustria.
Furtherannoyingproblemsobviouslyarise.EgwhenIliketomergetwoframesbasedonthecountry
column.One data.frame uses "Austria" whiletheotherframehas "Austria" .Thematching
doesn'twork.
1. Isthereanicewayto'show'thewhitespaceonmyscreensothatiamawareoftheproblem?
2. AndcanIremovetheleadingandtrailingwhitespaceinR?
SofarIusedtowriteasimple Perl scriptwhichremovesthewhitespacebutitwouldbeniceifIcan
somehowdoitinsideR.
r

editedNov28'13at1:49
JeromyAnglim
8,990 3 49 105

askedFeb14'10at12:44
mropa
3,078 2 17 24

Ijustsawthat sub() usesthe Perl notationaswell.Sorryaboutthat.Iamgoingtotrytousethefunction.


Butformyfirstquestionidon'thaveasolutionyet. mropa Feb14'10at12:50
Ashadleypointeditthisregex"^\\s+|\\s+$"willidentifyleadingandtrailingwhitespace.sox<
gsub("^\\s+|\\s+$","",x)manyofR'sreadfunctionsashavethisoption:strip.white=FALSEJayFeb14'10
at15:11
addacomment

5 Answers
Probablythebestwayistohandlethetrailingwhitespaceswhenyoureadyourdatafile.Ifyouuse
read.csv or read.table youcansettheparameter strip.white=TRUE .
Ifyouwanttocleanstringsafterwardsyouoneofthesefunctions:
#returnsstringw/oleadingwhitespace
trim.leading<function(x)sub("^\\s+","",x)
#returnsstringw/otrailingwhitespace
trim.trailing<function(x)sub("\\s+$","",x)
#returnsstringw/oleadingortrailingwhitespace
trim<function(x)gsub("^\\s+|\\s+$","",x)
Touseoneofthesefunctionson myDummy$country :

http://stackoverflow.com/questions/2261079/howtotrimleadingandtrailingwhitespaceinr

1/3

27/12/2014

HowtotrimleadingandtrailingwhitespaceinR?StackOverflow
myDummy$country<trim(myDummy$country)

To'show'thewhitespaceyoucoulduse:
paste(myDummy$country)
whichwillshowyouthestringssurroundedbyquotationmarks(")makingwhitespaceseasiertospot.
editedFeb14'10at15:52

answeredFeb14'10at13:13
f3lix
14.6k 7 37 64

@f3lixohthosearesomenicetips!thanks! mropa Feb14'10at13:43

3 Ashadleypointeditthisregex"^\\s+|\\s+$"willidentifyleadingandtrailingwhitespace.sox<
gsub("^\\s+|\\s+$","",x)manyofR'sreadfunctionsashavethisoption:strip.white=FALSEJayFeb14'10
at15:10

@Jay:Thanksforthehint.Ichangedtheregexpsinmyanswertousetheshorter"\\s"insteadof"[\t]".
f3lixFeb14'10at15:46

8 Seealso str_trim inthe stringr package.RichieCottonFeb16'10at15:35


1 Plusonefor"Trimfunctionnowstoredforfutureuse"thanks!ChrisBeeleyJan17'12at9:56
show2morecomments

Tomanipulatethewhitespace,usestr_trim()inthestringrpackage.ThepackagehasmanualdatedFeb
15,2013andisinCRAN.Thefunctioncanalsohandlestringvectors.
install.packages("stringr",dependencies=TRUE)
require(stringr)
example(str_trim)
d4$clean2<str_trim(d4$V2)
(creditgoestocommenter:R.Cotton)
answeredFeb21'13at16:30
user56
785 1 10 23

1 +1Forbestpractice,mosteasy,mostconvenientsolution!petermeissnerOct16at12:24
addacomment

ad1)Toseewhitespacesyoucoulddirectlycall print.data.frame withmodifiedarguments:


print(head(iris),quote=TRUE)
#Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
#1"5.1""3.5""1.4""0.2""setosa"
#2"4.9""3.0""1.4""0.2""setosa"
#3"4.7""3.2""1.3""0.2""setosa"
#4"4.6""3.1""1.5""0.2""setosa"
#5"5.0""3.6""1.4""0.2""setosa"
#6"5.4""3.9""1.7""0.4""setosa"
Seealso ?print.data.frame forotheroptions.
answeredFeb15'10at10:00
Marek
19.5k 5 38 62
addacomment

Asimplefunctiontoremoveleadingandtrailingwhitespace:
trim<function(x){
gsub("(^[[:space:]]+|[[:space:]]+$)","",x)
}
Usage:
>text="foobarbaz3"
>trim(text)
[1]"foobarbaz3"

http://stackoverflow.com/questions/2261079/howtotrimleadingandtrailingwhitespaceinr

2/3

27/12/2014

HowtotrimleadingandtrailingwhitespaceinR?StackOverflow
answeredFeb19at13:37
BernhardKausler
1,541 10 23
addacomment

Usegreporgrepltofindobservationswithwhitespacesandsubtogetridofthem.
names<c("GangaDin\t","ShyamLal","Bulbul")
grep("[[:space:]]+$",names)
[1]13
grepl("[[:space:]]+$",names)
[1]TRUEFALSETRUE
sub("[[:space:]]+$","",names)
[1]"GangaDin""ShyamLal""Bulbul"
answeredFeb14'10at14:13
JyotirmoyBhattacharya
2,939 1 13 25

4 Or,alittlemoresuccinctly, "^\\s+|\\s+$" hadleyFeb14'10at14:45


1 Justwantedtopointout,thatonewillhavetouse gsub insteadof sub withhadley'sregexp.With sub it
willstriptrailingwhitespaceonlyifthereisnoleadingwhitespace...f3lixFeb14'10at15:50

Didn'tknowyoucoulduse\setc.withperl=FALSE.ThedocssaythatPOSIXsyntaxisusedinthatcase,but
thesyntaxacceptedisactuallyasupersetdefinedbytheTREregexlibrary
laurikari.net/tre/documentation/regexsyntaxJyotirmoyBhattacharyaFeb14'10at18:37

addacomment

Not the answer you're looking for? Browse other questions tagged r or ask your own
question.

http://stackoverflow.com/questions/2261079/howtotrimleadingandtrailingwhitespaceinr

3/3

You might also like