[S] A casefold() function that caps only the first character

John Wallace (jrw@fish.washington.edu)
Wed, 16 Sep 1998 12:07:29 -0700 (PDT)


A function that changes character strings with only the first
character in upper case is attached below.

> casefold.f(c("mIke", "jANe", "TOdd"))
[1] "Mike" "Jane" "Todd"

> casefold.f(c("mIke", "jANe", "TOdd"), first=F)
[1] "mike" "jane" "todd"

> casefold.f(c("mIke", "jANe", "TOdd"), first=F, upper=T)
[1] "MIKE" "JANE" "TODD"

___________________________________

casefold.f <- function(cv, first.letter.cap = T, upper = F)
{
# DATE WRITTEN: 15 Sept 1998
# Author: John R. Wallace (jrw@fish.washington.edu)
#
case.f <- function(x, first.letter.cap = T, upper = F)
{
if(first.letter.cap) {
x.1 <- substring(x, 1, 1)
x.rest <- substring(x, 2, nchar(x))
paste(c(c(LETTERS, LETTERS)[match(x.1, c(
LETTERS, letters))], c(letters,
letters)[match(substring(x.rest, 1:
nchar(x.rest), 1:nchar(x.rest)), c(
LETTERS, letters))]), collapse = ""
)
}
else {
if(upper)
paste(c(LETTERS, LETTERS)[match(
substring(x, 1:nchar(x), 1:nchar(
x)), c(LETTERS, letters))],
collapse = "")
else paste(c(letters, letters)[match(
substring(x, 1:nchar(x), 1:nchar(
x)), c(LETTERS, letters))],
collapse = "")
}
}
apply(matrix(cv), 1, case.f, first.letter.cap =
first.letter.cap, upper = upper)
}

-- 
John Wallace
University of Washington                    ^    ^    ^
Fisheries Research Institute               / \  / \  / \   ^
Box 357980                                 / \  / \   |   / \
Seattle, WA 98195-7980                      |    |  o__~  / \
PHONE   (206) 543-1513                  @ @         /\/\   |
FAX     (206) 685-7471                   ~    
E-MAIL        jw@u.washington.edu              o
WWW      http://www.fish.washington.edu/people/jrw/Wallace.html 
                                                o  _///_ //
                                                <`)=  _<<
                                                    \\\  \\

----------------------------------------------------------------------- This message was distributed by s-news@wubios.wustl.edu. To unsubscribe send e-mail to s-news-request@wubios.wustl.edu with the BODY of the message: unsubscribe s-news