[R] How to remove white spaces

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Aug 16 05:13:43 CEST 2008


On Fri, 15 Aug 2008, Bos, Roger wrote:

> All,
>
> I am storing a string of email addresses in a table and when I retrieve
> them using a RODBC statement such as:
>
> test = sqlQuery(xf, "select specialist from
> roger_sector_specialist")$specialist
>
> 'test' is of mode list and includes a bunch of white space.

I don't see how: sqlQuery returns a data frame, and this is one column of 
it.  You can have 'list' columns in data frames, but not from sqlQuery.

> As a work-around I convert 'test' to a character string, split it based 
> on a white space, then take the first element:
>
> test = strsplit(as.character(test), " ")
> emaillist = test[[1]][1]
>
> But I am sure there is a better way.  I did help.search("trim white
> space") and R didn't return anything, which I found strange.  Any
> advice?

help.search() is not a full-text search.  RSIteSearch("trim white space") 
give lots of relevant info (including the difference between " " and white 
space).

See ?gsub for trimming trailing whitespace.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list