Hello, I am working with R version 2.10.1 under windows. In a text file, I need to replace all characters at certain column positions with blanks. For example, say the file contains two lines and looks like this: ab34cd78e fg3 hi78j I'd like to replace everything at positions 3-4 and 7-8 with blanks, so the output should be: ab cd e fg hi j [I'm not sure if this is really an R question(?), solutions outside of R - maybe via shell() or so - are welcome!] Thanks, Heinrich.
I'd just import the positions I like using read.fwf() or otherwise you can replace strings at certain positions, see ?substr Uwe Ligges On 28.10.2010 11:26, RINNER Heinrich wrote:> Hello, > > I am working with R version 2.10.1 under windows. > In a text file, I need to replace all characters at certain column positions with blanks. > For example, say the file contains two lines and looks like this: > > ab34cd78e > fg3 hi78j > > I'd like to replace everything at positions 3-4 and 7-8 with blanks, so the output should be: > > ab cd e > fg hi j > > [I'm not sure if this is really an R question(?), solutions outside of R - maybe via shell() or so - are welcome!] > > Thanks, > Heinrich. > > ______________________________________________ > 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.
On Thu, Oct 28, 2010 at 5:26 AM, RINNER Heinrich <HEINRICH.RINNER at tirol.gv.at> wrote:> Hello, > > I am working with R version 2.10.1 under windows. > In a text file, I need to replace all characters at certain column positions with blanks. > For example, say the file contains two lines and looks like this: > > ab34cd78e > fg3 hi78j > > I'd like to replace everything at positions 3-4 and 7-8 with blanks, so the output should be: > > ab ?cd ?e > fg ?hi ?j > > [I'm not sure if this is really an R question(?), solutions outside of R - maybe via shell() or so - are welcome!] >Try this:> s <- c("ab34cd78e", "fg3 hi78j") > sub("^(..)..(..)..", "\\1 \\2 ", s)[1] "ab cd e" "fg hi j" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com