Re: [R] How to vectorize

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 07 Jun 2005 - 12:36:14 EST

On 6/6/05, PANTERA Laurent <laurent.pantera@irsn.fr> wrote:
> Dear R-List,
>
> I would like to write nicely the names of some isotopes on a plot. The
> code bellow works fine.
>
> plot(1:10,1:10)
> text(c(2,4,8),c(2,4,8),labels=c(expression(italic(phantom(0)^{78}*Ge)),
> expression(italic(phantom(0)^{137}*Cs)),
> expression(italic(phantom(0)^{129*m}*Te))),
> cex=3
> )
>
> But, since I have a lot of isotopes to write on the plot, I would like
> to construct automatically the labels. So I wrote the code below which
> works fine.
>
> listenoms <- list(nom=c("Ge","Cs","Te"),num=c("78","137","129*m"))
> n <- length(listenoms$nom)
> resu <- "c("
> for( i in 1:(n-1))
> {
> resu <- paste(resu,paste("expression(italic(phantom(0)^{",
> listenoms$num[i],"}*",
> listenoms$nom[i],")),",sep=""))
> }
> resu <- paste(resu,paste("expression(italic(phantom(0)^{",
> listenoms$num[n],"}*",
> listenoms$nom[n],")))",sep=""))
> plot(1:10,1:10)
> text(c(2,4,8),c(2,4,8),labels=eval(parse(text=resu)),cex=2)
>
> I assume there is a better way to do that using vectorization.
> May you help me to find it ?
>

Assuming your setup:

x <- c(2,4,8)
nom <- c("Ge","Cs","Te")
num <- c("78","137","129*m")
plot(1:10)

# the ith label can be generated via
lab <- function(i)
  as.expression(bquote(italic(phantom(0)^{.(num[i])}*.(nom[i]))))

# giving the following vectorized solution labs <- lapply(seq(x), lab)
text(x,x,do.call(c,labs))

# although a 'for' loop is arguably simpler plot(1:10)
for(i in seq(x)) text(x[i], x[i], lab(i))



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 07 12:41:40 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:25 EST