Re: [R] strip white in character strings

From: Scillieri, John <John.Scillieri_at_constellation.com>
Date: Wed, 14 May 2008 13:36:13 -0400

> a = " United Kingdom "
> gsub( " ", "", a, fixed=TRUE )

[1] "UnitedKingdom"

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Jeremiah Rounds
Sent: Wednesday, May 14, 2008 12:48 PM
To: 'r-help_at_stat.math.ethz.ch'
Subject: Re: [R] strip white in character strings

> Date: Wed, 14 May 2008 12:06:39 -0400
> From: roland.rproject_at_gmail.com
> To: r-help_at_stat.math.ethz.ch
> Subject: [R] strip white in character strings
>
> Dear all,
>
> I have several datasets and I want to generate pdf plots from them.
> I also want to generate automatically the names of the files. They are

> country-specific and the element mycurrentdata[1,1] contains this
> information.
>
> So what I do is something like this:
> pdf(file=paste(mycurrentdata[1,1], ".pdf", sep=""), width=...etc)
>
> The only problem I have is that some of the country names contain
> white space (e.g., "United Kingdom"). This is no problem for
> generating the pdf plots but it may become problematic during further
processing (e.g.
> incl. the plots in LaTeX documents).
>
> Is there an easy function to strip white space out of character
> strings (similar to the strip.white=TRUE option in read.table/scan)?

How about

> a = " United Kingdom "
> paste(unlist(strsplit(a,split=" ")), collapse="")
[1] "UnitedKingdom"

Note better might is using generic trimming functions after the split to catch any left over non-space white space stuff in each split.

>
> I'd appreciate any kind of help and I hope I did not miss anything
> completely obvious.
>
> Thanks,
> Roland
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


 1.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
>>> This e-mail and any attachments are confidential, may contain legal, professional or other privileged information, and are intended solely for the addressee. If you are not the intended recipient, do not use the information in this e-mail in any way, delete this e-mail and notify the sender. CEG-IP1


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 14 May 2008 - 21:20:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 14 May 2008 - 21:30:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive