I feel like an idiot posting this because every language I've ever seen has a string function that trims blanks off strings (off the front or back or both). Ideally, it would process whole data frames/matrices etc but I don't even see one that processes a single string. But I've searched and I don't even see that. There's a strtrim function but it does something completely different. -- View this message in context: http://www.nabble.com/Function-for-trim-blanks-from-a-string%28s%29--tf4226133.html#a12022374 Sent from the R help mailing list archive at Nabble.com.
On Mon, 2007-08-06 at 12:15 -0700, adiamond wrote:> I feel like an idiot posting this because every language I've ever seen has a > string function that trims blanks off strings (off the front or back or > both). Ideally, it would process whole data frames/matrices etc but I don't > even see one that processes a single string. But I've searched and I don't > even see that. There's a strtrim function but it does something completely > different.If you want to do this while initially importing the data into R using one of the read.table() family of functions, see the 'strip.white' argument in ?read.table, which would do an entire data frame in one call. Otherwise, the easiest way to do it would be to use sub() or gsub() along the lines of the following: # Strip leading space sub("^ +", "", YourTextVector) # Strip trailing space sub(" +$", "", YourTextVector) # Strip both gsub("(^ +)|( +$)", "", YourTextVector) Examples of use:> sub("^ +", "", " Leading Space")[1] "Leading Space"> sub(" +$", "", "Trailing Space ")[1] "Trailing Space"> gsub("(^ +)|( +$)", "", " Leading and Trailing Space ")[1] "Leading and Trailing Space" See ?sub which also has ?gsub Note that the above will only strip spaces, not all white space. You can then use the appropriate call in one of the *apply() family of functions to loop over columns/rows as may be appropriate. HTH, Marc Schwartz
Use trim from: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/40714.html or trim from gdata or trimWhiteSpace from limma in BioConductor. RSiteSearch("trim") and help.search("trim") can locate such functions. s <- c(" abc ", " def ") # vector m <- cbind(a = s, b = s) # matrix DF <- as.data.frame(m) # data.frame trim(s) trim(m) DF[] <- lapply(DF, trim) On 8/6/07, adiamond <adiamond at csidentity.com> wrote:> > I feel like an idiot posting this because every language I've ever seen has a > string function that trims blanks off strings (off the front or back or > both). Ideally, it would process whole data frames/matrices etc but I don't > even see one that processes a single string. But I've searched and I don't > even see that. There's a strtrim function but it does something completely > different. > -- > View this message in context: http://www.nabble.com/Function-for-trim-blanks-from-a-string%28s%29--tf4226133.html#a12022374 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
For all the quick responses that have been more than sufficient to get through. -- View this message in context: http://www.nabble.com/Function-for-trim-blanks-from-a-string%28s%29--tf4226133.html#a12026177 Sent from the R help mailing list archive at Nabble.com.
Stripping either or both ends can be achieved by the somewhat tortuous but fairly general stripspace<-function(x) sub("^\\s*([^ ]*.*[^ ])\\s*$", "\\1",x) which uses a replacement buffer to keep the useful part of the string.>>> Prof Brian Ripley <ripley at stats.ox.ac.uk> 06/08/2007 21:23:49 >>>I am sure Marc knows that ?sub has examples of trimming trailing space and whitespace in various styles. ******************************************************************* This email contains information which may be confidential and/or privileged, and is intended only for the individual(s) or organisation(s) named above. If you are not the intended recipient, then please note that any disclosure, copying, distribution or use of the contents of this email is prohibited. Internet communications are not 100% secure and therefore we ask that you acknowledge this. If you have received this email in error, please notify the sender or contact +44(0)20 8943 7000 or postmaster at lgcforensics.com immediately, and delete this email and any attachments and copies from your system. Thank you. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex TW11 0LY, UK