I've been using gsub to abbreviate words in longer strings. I'd like to abbreviate a word and then inherit as much of the capitalization of the input as I can.

Example, turn hello to hi in this:

```
x <- c("Hello World", "HELLO WORLD", "hello world", "hElLo world")

```

But respect the case of hello in the original

```
c("Hi World", "HI WORLD", "hi world", "hI world")

```

Most of the examples I really want to match are "HI" "hi" and "Hi". I don't care so much about "hI", but for completeness, I leave that as a possibility. 

To get this done until now, I have the tedious approach of maintaining vectors of strings of targets and replacements

```
xin <- c("Hello\ ", "HELLO\ ", "hello\ ", "hElLo\ ")
xout <- c("Hi ", "HI ", "hi ", "hI ")
mapply(gsub, xin, xout, x)

```

That gives a correct answer, see:

```
     Hello      HELLO      hello      hElLo
"Hi World" "HI WORLD" "hi world" "hI world"

```

But this is embarrassing and time consuming and inflexible! So far, I have a family of 50 words for which we seek abbreviation, and keeping all of the case combinations is tiresome.

The data is full of mixed-case data chaos because humans typed in about 78000 records and they capitalized words like department and university in every conceivable way. The long sentences they typed don't fit in the space allowed on the printed page, and we are asked to shorten them to "dept" and "univ". We want to preserve the capitalization if possible.

The only idea I have looks not much like R to me. Split the original input, tabulate the existing capitalization for the first 2 letters.

```
xcap <- sapply(strsplit(x, split = ""), function(x) x %in% LETTERS)[1:2, ]
> t(xcap)
      [,1]  [,2]
[1,]  TRUE FALSE
[2,]  TRUE  TRUE
[3,] FALSE FALSE
[4,] FALSE  TRUE

```

I'm pretty sure I could use that capitalization information to make this work right. But I haven't yet succeeded. I've just become aware of G Grothendieck's package gsubfn which might work, but the terminology there ("proto" objects) is new to me.

I'll keep going in that direction, probably, but am asking now if there is a more direct route.

pj