Hi, Thanks for your reply, my names column has since been up dated to the following A_mgkg, L_., B_mgkg, S_. etc.
And I need the ouput as folllows: -1 A (mg kg ) L (%) -1 B (mg kg ) S (%) I used the function first created by William and then applied lapply and then tried using a for loop as follows: f <- function (name) { # add other suffices and their corresponding plotmath expressions to the list env <- list2env(list(mgkg = bquote(mg ~ kg^{-1}), ugkg = bquote(mu * g ~ kg^{-1})), parent = emptyenv()) pattern <- paste0("_(", paste(objects(env), collapse="|"), ")") bquoteExpr <- parse(text=gsub(pattern, "~(.(\\1))", name))[[1]] # I use do.call() to work around the fact that bquote's first argument is not evaluated. do.call(bquote, list(bquoteExpr, env)) } DATA_names<-lapply(DATA_names,function(x) if(grepl("_mgkg",x)) f(x) else x)[1:length(DATA_names)] for (i in one:length(DATA_names)) if ((grepl("_.",DATA_names[i]))=="TRUE") DATA_names[i]<-gsub("_[.]"," (%)",DATA_names[i]) This works but what actuall gets plotted on the graph is A ~ (mg ~ kg^{ -1 }) And not -1 A (mg kg ) Which I cant understand? Thanks On Thu, Apr 4, 2013 at 1:50 PM, arun <smartpink...@yahoo.com> wrote: > Hi, > DATA_names<-c("A_mgkg","B_mgkg","C_mgkg","D_mgkg","E_mgkg","F_mgkg","G_mgkg","H > mgkg") > > lapply(DATA_names,function(x) if(grepl("_",x)) f(x) else x)[1:2] > #[[1]] > #A ~ (mg ~ kg^{ > # -1 > #}) > > #[[2]] > #B ~ (mg ~ kg^{ > # -1 > #}) > > > f("Na_mgkg") > #Na ~ (mg ~ kg^{ > # -1 > #}) > > A.K. > > ----- Original Message ----- > From: Shane Carey <careys...@gmail.com> > To: William Dunlap <wdun...@tibco.com> > Cc: "r-help@r-project.org" <r-help@r-project.org> > Sent: Thursday, April 4, 2013 5:39 AM > Subject: Re: [R] Superscript > > Hi William, > > Im trying to run this function within a for loop as follows: > > f <- function (name) > { > # add other suffices and their corresponding plotmath expressions to the > list > env <- list2env(list(mgkg = bquote(mg ~ kg^{-1}), > ugkg = bquote(mu * g ~ kg^{-1})), > parent = emptyenv()) > pattern <- paste0("_(", paste(objects(env), collapse="|"), ")") > bquoteExpr <- parse(text=gsub(pattern, > "~(.(\\1))", > name))[[1]] > # I use do.call() to work around the fact that bquote's first argument is > not evaluated. > do.call(bquote, list(bquoteExpr, env)) > } > > for (i in one:length(DATA_names)) > if ((grepl("_",DATA_names[i]))=="TRUE") > DATA_names[i]<-f(DATA_names[i])) > > I keep getting an error saying: incompatible types (from symbol to > character) in subassignment type fix > > Have you any ideas on how to get around this, thanks again for your help, > much appreciated. > Cheers > > > On Wed, Apr 3, 2013 at 5:33 PM, William Dunlap <wdun...@tibco.com> wrote: > > > Are you trying to convert a column name like "Na_mgkg" to a plot label > > like Na (mg kg^-1) ? > > If so you will have to use both string manipulation functions like gsub() > > and expression manipulating > > functions like bquote(). E.g., > > > > f <- function (name) > > { > > # add other suffices and their corresponding plotmath expressions to > > the list > > env <- list2env(list(mgkg = bquote(mg ~ kg^{-1}), > > ugkg = bquote(mu * g ~ kg^{-1})), > > parent = emptyenv()) > > pattern <- paste0("_(", paste(objects(env), collapse="|"), ")") > > bquoteExpr <- parse(text=gsub(pattern, > > "~(.(\\1))", > > name))[[1]] > > # I use do.call() to work around the fact that bquote's first argument > > is not evaluated. > > do.call(bquote, list(bquoteExpr, env)) > > } > > > > d <- data.frame("Na_mgkg"=1:10, "K_ugkg"=10:1) > > plot(Na_mgkg ~ K_ugkg, data=d, xlab=f("K_ugkg"), ylab=f("Na_mgkg")) > > > > Bill Dunlap > > Spotfire, TIBCO Software > > wdunlap tibco.com > > > > > > > -----Original Message----- > > > From: r-help-boun...@r-project.org [mailto: > r-help-boun...@r-project.org] > > On Behalf > > > Of Shane Carey > > > Sent: Wednesday, April 03, 2013 8:02 AM > > > To: r-help@r-project.org > > > Subject: [R] Superscript > > > > > > Hi, > > > How do I write a superscript within gsub? > > > > > > I have the following: gsub("_mgkg",expression(paste("mg > > kg"^{-1})),names[1]) > > > > > > Thanks > > > > > > > > > > > > -- > > > Shane > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help@r-project.org mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > -- > Shane > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > -- Shane [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.