[R] Replace only Capital Letters

Rau, Roland Rau at demogr.mpg.de
Thu Jul 22 19:17:36 CEST 2004


Dear All,

I have these data:

exampledata <- c("This is one item", "This is Another One", "And so is
This")

I would like to find each occurence of a blank space followed by a Capital
Letter and replace it by a blank space, a left curly brace, the respective
Capital Letter, and then a right curly brace.

I thought the following will do:
gsub(pattern = " ([A-Z])", replacement = " {\\1}", x=exampledata,
ignore.case=FALSE)

Unfortunately, the actual output was:
"This {i}s {o}ne {i}tem"    "This {i}s {A}nother {O}ne" "And {s}o {i}s
{T}his"  

But what I wanted was actually:
"This is one item"    "This is {A}nother {O}ne" "And so is {T}his"  

Can anyone tell me what I should change. Should be fairly easy for people
with more experience than me using regular expressions, I guess.

Thanks,
Roland

P.S. The background is my bibliography-file for BibTeX. If the title field
has some content like "An analysis of Denmark", it would actually turn out
to be "An analysis of denmark" in my dvi-document. Of course, R is not the
appropriate tool for this. But apart from the little problem outlined above,
I had a function doing what I wanted in less than 10 minutes.


+++++
This mail has been sent through the MPI for Demographic Rese...{{dropped}}




More information about the R-help mailing list