How To Trim Leading and Trailing Whitespace in R - Stack Overflow
How To Trim Leading and Trailing Whitespace in R - Stack Overflow
login
StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,no
registrationrequired.
tour
HowtotrimleadingandtrailingwhitespaceinR?StackOverflow
27/12/2014
help
stackoverflowcareers
Takethe2minutetour
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
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
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
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
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
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