[R] Labelling

arun smartpink111 at yahoo.com
Tue Jul 9 15:36:58 CEST 2013


Hi,
Try this:
f1<- function(name)
{
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("_"," ",gsub(pattern,"~(.(\\1))~",name)))[[1]]
do.call(bquote, list(bquoteExpr, env))
}
sapply(DATA_names,f1)
$A_ugkg_FA
A ~ (mu * g ~ kg^{
    -1
}) ~ FA

$S_mgkg_XRF
S ~ (mg ~ kg^{
    -1
}) ~ XRF

$Cl_mgkg_XR
Cl ~ (mg ~ kg^{
    -1
}) ~ XR

A.K.






________________________________
From: Shane Carey <careyshan at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Tuesday, July 9, 2013 8:57 AM
Subject: Re: [R] Labelling



Initially, I wanted to remove the suffixes, but now I want to end up with the following 

c("A_ugkg_FA","S_mgkg_XRF" ,"Cl_mgkg_XR")


             -1
A (ug kg    ) FA


             -1
S (mg kg   ) XRF


              -1
Cl (mg kg   ) XR

Thanks all



On Tue, Jul 9, 2013 at 1:48 PM, arun <smartpink111 at yahoo.com> wrote:

Hi,
>May be this helps:
>
> gsub("_"," ",gsub("(.*)_.*","\\1",DATA_names))
>#[1] "A ugkg"  "S mgkg"  "Cl mgkg"
>sapply(gsub("_"," ",gsub("(.*)_.*","\\1",DATA_names)),f)
>$`A ugkg`
>A ~ (mu * g ~ kg^{
>    -1
>})
>
>$`S mgkg`
>S ~ (mg ~ kg^{
>    -1
>})
>
>$`Cl mgkg`
>Cl ~ (mg ~ kg^{
>    -1
>})
>
>
>A.K.
>
>
>----- Original Message -----
>From: Shane Carey <careyshan at gmail.com>
>To: "r-help at r-project.org" <r-help at r-project.org>
>Cc:
>Sent: Tuesday, July 9, 2013 7:20 AM
>Subject: [R] Labelling
>
>Hi,
>
>I have the following data as labels:
>
>DATA_names<-c("A_ugkg_FA","S_mgkg_XRF" ,"Cl_mgkg_XR")
>
>and I need to convert to
>
>
>             -1
>A (ug kg     )
>
>             -1
>S (mg kg    )
>
>              -1
>Cl (mg kg    )
>
>
>I used the following piece of code to convert the following labels in the
>past, but cant get it to work for the new labels:
>
>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))
>}
>
>The labels in the past were:
>DATA_names<-c("A_ugkg","S_mgkg" ,"Cl_mgkg")
>
>Thanks
>
>--
>Shane
>
>
>    [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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



More information about the R-help mailing list