[R] Symbolic substitution in parallel; use infinity symbol?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Dec 31 02:35:37 CET 2007


John Maindonald wrote:
> I'd like to be able to modify axlab in (C) below so that 'Inf'
> is replaced by the infinity symbol.
>
> y <- rnorm(40)
> breaks <- c(-Inf, -1, 1, Inf)
> x <- cut(y, breaks=breaks)
> plot(unclass(x), y, xaxt="n", xlab="")
>
> ## A: The following gives the axis labels "(-Inf, 1]", etc.
> axis(1, at=1:3, labels=expression("(-Inf,-1]", "(-1,1]", "(1, Inf]"))
>
> ## B: The following replaces Inf by the infinity symbol
> axis(1, at=1:3, labels=expression("(" * -infinity * ", " * -1 * "]",
>                                  "(" * -1 * ", 1]", "(1, " * infinity  
> * "]"),
>     line=1.25, lty=0)
>
> ## C: The following gives the axis labels "(-Inf, 1]", etc.,
> ## in a more automated manner.
> axlab <- lapply(levels(x), function(x)substitute(a, list(a=x)))
> # Can alternatively use bquote()
> axis(1, at=1:3, labels=as.expression(axlab), line=2.25, lty=0)
>
> However I have been unable to modify axlab so that the infinity
> symbol appears in place of 'Inf'.  Is there is some relatively
> straightforward way to do this?  The issue is of course more
> general than this specific example.
>   
Here's an idea, leaving some tinkering for you:

breaks <- c(-Inf, -1, 1, Inf)

zz <- lapply(breaks, function(x) 
      if(x==-Inf) quote(-infinity) else
      if (x==Inf) quote(infinity) else 
      format(x))

lbl <- mapply(function(x,y)
                 bquote("(" * .(x) * "," * .(y) * "]"),
              zz[-4], zz[-1], SIMPLIFY=FALSE)


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list