ordinal(): [was "a handy function ..." in March..]

Martin Maechler Martin Maechler <maechler@stat.math.ethz.ch>
Wed, 1 Jul 1998 18:30:42 +0200


I'm finally cleaning up old things / todo's;
We had about half a dozen e-mails on R-devel back in mid March...... 
Here is my proposal, a sometimes useful utility for constructing strings
in cat() or text(), legend(), etc.:


ordinal <-
function(i, language =3D "english", gender =3D c("female","male"), sep=3D""=
) {
    ii <- i
    if(!is.numeric(i) || any(i < 0) || any(ii !=3D (i <- as.integer(i))))
      stop("'i' must be non-negative integer")
    ilang <- pmatch(language, c("english", "francais", "deutsch",
                                "italiano", "espanol")) #, "portugues" ?
    if(is.na(ilang))
      stop(paste("language '", language, "' not (yet) implemented.", sep=3D=
""))
    ig <- pmatch(gender, c("female","male"))[1]
    if(is.na(ig)) stop(paste("gender '", gender, "' is invalid.", sep=3D"")=
)
    suffix <- switch(ilang,
                     c("st","nd","rd","th")[pmin(4,ifelse(i<20,ifelse(i=3D=
=3D0,4,i),
                                                          1+(i-1)%%10))],# =
engl
                     c(c("=E8re","er")[ig],"me")[1 + (i!=3D1)],# french
                     ".",# german
                     c("a","o")[ig], # italian
                     c("a","o")[ig], # spanish
                     )
    paste(i, suffix, sep=3Dsep)
}

nn <- c(0:3,9:13,20:22,99:102)


---------------------------------------------------------------------------=
---
-------------------- Test a few interesting cases ----------------
---------------------------------------------------------------------------=
---

-------------------- Is this correct/acceptable for the given languages? 
--------------------
-------------------- Additions, bugs, ``patches'' ?

> ordinal(1:22)
 [1] "1st"  "2nd"  "3rd"  "4th"  "5th"  "6th"  "7th"  "8th"  "9th"  "10th"
[11] "11th" "12th" "13th" "14th" "15th" "16th" "17th" "18th" "19th" "20th"
[21] "21st" "22nd"
> ordinal(nn,"english")
 [1] "0th"   "1st"   "2nd"   "3rd"   "9th"   "10th"  "11th"  "12th"  "13th"=
 
[10] "20th"  "21st"  "22nd"  "99th"  "100th" "101st" "102nd"
> ordinal(nn,"francais")
 [1] "0me"   "1=E8re"  "2me"   "3me"   "9me"   "10me"  "11me"  "12me"  "13m=
e" 
[10] "20me"  "21me"  "22me"  "99me"  "100me" "101me" "102me"
> ordinal(1:3,"fr","m")
[1] "1er" "2me" "3me"
> ordinal(nn, "italian")# Ladies first =3D> Ladies default...
 [1] "0a"   "1a"   "2a"   "3a"   "9a"   "10a"  "11a"  "12a"  "13a"  "20a" 
[11] "21a"  "22a"  "99a"  "100a" "101a" "102a"
> ordinal(nn, "espan", "m")
 [1] "0o"   "1o"   "2o"   "3o"   "9o"   "10o"  "11o"  "12o"  "13o"  "20o" 
[11] "21o"  "22o"  "99o"  "100o" "101o" "102o"
> 
> ordinal(nn, "deutsch", "m")
 [1] "0."   "1."   "2."   "3."   "9."   "10."  "11."  "12."  "13."  "20." 
[11] "21."  "22."  "99."  "100." "101." "102."
> 
> ## These give an error
> ordinal(pi)
Error: 'i' must be non-negative integer
Execution halted
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._