Re: [R] Remove a word from a character vector value XXXX
On Mar 7, 2012, at 11:03 AM, Dan Abner wrote: Hi everyone, What is the easiest way to remove the word Average and strip leading and trailing blanks from the character vector (d5.Region) below? .nrow.d5. d5.Region 11 22 Coastal Average 33East Average 44 Metro East Average 55 Metro North Average 66 Metro South Average 77 Metro West Average 88 Northeast Average 99 Northwest Average Not sure which leading or trailing spaces you mean. Once cannot infer such structure from console output. Provide a reproducible example with dput() if you want better answers. > sub( "\\s+Average.+$", "", "CentralAverage junk ") [1] "Central" This would strip space before 'Average' and anything after it. -- David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Remove a word from a character vector value XXXX
Hadley's package stringr is wonderful for all things string. library(stringr) ?str_trim and ?str_replace are what you want. (the base R equivalent of these two would be ?gsub and some regular expressions) str_trim(str_replace(d5.Region, 'Average', '')) should do the trick. hope that helps, Justin On Wed, Mar 7, 2012 at 8:03 AM, Dan Abner wrote: > Hi everyone, > > What is the easiest way to remove the word Average and strip leading > and trailing blanks from the character vector (d5.Region) below? > > .nrow.d5. d5.Region > 1 1 Central Average > 2 2 Coastal Average > 3 3 East Average > 4 4 Metro East Average > 5 5 Metro North Average > 6 6 Metro South Average > 7 7 Metro West Average > 8 8 Northeast Average > 9 9 Northwest Average > > > Thanks! > > Dan > > __ > R-help@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. __ R-help@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.
[R] Remove a word from a character vector value XXXX
Hi everyone, What is the easiest way to remove the word Average and strip leading and trailing blanks from the character vector (d5.Region) below? .nrow.d5. d5.Region 11 Central Average 22 Coastal Average 33East Average 44 Metro East Average 55 Metro North Average 66 Metro South Average 77 Metro West Average 88 Northeast Average 99 Northwest Average Thanks! Dan __ R-help@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.