Some years ago when I used S-PLUS, I seem to remember, there was a discussion about a simple method of removing trailing spaces from vector elements. I'd be fairly sure the same would work in R, but I can't find any mention of anything like it in the R archives or with help.search(). There are ways I could do it with substring(), but I seem to remember there was something more elegant. I can find the strip function in the clim.pact package, but that trims everything after the first space which is not useful in this case. Someone please refresh my memory. TIA -- Patrick Connolly HortResearch Mt Albert Auckland New Zealand Ph: +64-9 815 4200 x 7188 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ I have the world`s largest collection of seashells. I keep it on all the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
Use regular expressions: ?regexpr
e.g. if myvec is your original vector of strings
notrlblanks<-sub('[[:blank:]]+?','',myvec)
-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
"The business of the statistician is to catalyze the scientific learning
process." - George E. P. Box
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
> Patrick Connolly
> Sent: Tuesday, December 21, 2004 2:39 PM
> To: R-help
> Subject: [R] Removing trailing spaces
>
> Some years ago when I used S-PLUS, I seem to remember, there was a
> discussion about a simple method of removing trailing spaces from
> vector elements. I'd be fairly sure the same would work in R, but I
> can't find any mention of anything like it in the R archives or with
> help.search().
>
> There are ways I could do it with substring(), but I seem to remember
> there was something more elegant. I can find the strip function in
> the clim.pact package, but that trims everything after the first space
> which is not useful in this case.
>
> Someone please refresh my memory.
>
> TIA
>
> --
> Patrick Connolly
> HortResearch
> Mt Albert
> Auckland
> New Zealand
> Ph: +64-9 815 4200 x 7188
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ~.~.~.~.~
> I have the world`s largest collection of seashells. I keep it on all
> the beaches of the world ... Perhaps you`ve seen it.
> ---Steven Wright
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ~.~.~.~.~
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
On Wed, 2004-12-22 at 11:38 +1300, Patrick Connolly wrote:> Some years ago when I used S-PLUS, I seem to remember, there was a > discussion about a simple method of removing trailing spaces from > vector elements. I'd be fairly sure the same would work in R, but I > can't find any mention of anything like it in the R archives or with > help.search(). > > There are ways I could do it with substring(), but I seem to remember > there was something more elegant. I can find the strip function in > the clim.pact package, but that trims everything after the first space > which is not useful in this case. > > Someone please refresh my memory. > > TIASee the final examples in ?sub HTH, Marc Schwartz
I generally use sub like so sub(" *$","",vector)
> x <- c("this one ","and that one ","plus
this")
> sub(" *$","",x)
[1] "this one" "and that one" "plus this"
>
Tom
Patrick Connolly wrote:> Some years ago when I used S-PLUS, I seem to remember, there was a
> discussion about a simple method of removing trailing spaces from
> vector elements. I'd be fairly sure the same would work in R, but I
> can't find any mention of anything like it in the R archives or with
> help.search().
>
> There are ways I could do it with substring(), but I seem to remember
> there was something more elegant. I can find the strip function in
> the clim.pact package, but that trims everything after the first space
> which is not useful in this case.
>
> Someone please refresh my memory.
>
> TIA
>
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Patrick Connolly <p.connolly at hortresearch.co.nz> writes:> Some years ago when I used S-PLUS, I seem to remember, there was a > discussion about a simple method of removing trailing spaces from > vector elements. I'd be fairly sure the same would work in R, but I > can't find any mention of anything like it in the R archives or with > help.search(). > > There are ways I could do it with substring(), but I seem to remember > there was something more elegant. I can find the strip function in > the clim.pact package, but that trims everything after the first space > which is not useful in this case. > > Someone please refresh my memory.Like this?> x <- c("a b ", "dfhskf ", "FGF", "IUOI UOUO UOUO ") > sub(" *$", "", x)[1] "a b" "dfhskf" "FGF" "IUOI UOUO UOUO" -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Apparently Analagous Threads
- Possible to get a definition of a function from a package to use without invoking the package?
- doubt in climate variability analysis in R!
- Problems with Raster and clim.pact packages with large netcdf files (2.7G) in x64 bit R
- Problem with running installed package on Windows
- Help with downloading clim.pact