[R] characters, mathematical expressions and computed values

David Winsemius dwinsemius at comcast.net
Sat Nov 24 18:39:42 CET 2012


On Nov 24, 2012, at 5:20 AM, mee1d3hs wrote:

> I still can't get this to work,

I sent a message yesterday that indicated that one of you earlier  
attempts was successful on my machine running the same (or roughly the  
same OS) and a current version of R. You seem to be ignoring the  
possibility that your R installation is broken.

> I am just trying to learn and this is
> supposed to be a feature of R, the ability to combine math notation in
> charts and exhibits
>
> I did some more work to try to show what I am trying it to and what  
> is not
> working
>
> x <- rnorm(1000,mean=10,sd=2)
>
> par(mfcol=c(3,3))
> meanx <- round(mean(x),digits=2)
>
>  x <- rnorm(1000,mean=10,sd=2)
>
> par(mfcol=c(4,2))
> meanx <- round(mean(x),digits=2)
>
> #chart 1:
> hist(x,main=paste(" 1. Mean of X =",bquote(.(meanx))))
> #chart 2:
> hist(x,main=paste(" 2. Mean of X =  
> ",substitute(meanx,list(meanx=meanx))))
> #chart 3:
> hist(x,main=expression("3. "*bar(x)*" = "))

This should "work", at least to the extent of putting nothing  after  
the equal sign. As should this:

hist(x,main=expression(3.~~bar(X)==phantom() ) )

The bquote function allows you to specify what portion of the  
expression will be evaluated. Otherwise everything gets displayed as  
typed (or interpreted by the plotmath evaluation engine). The tilde  
and asterisk are plotmath separators. Using them to bind together  
plotmath or text items will generally be much more readable than using  
paste.


> #chart 4:
> hist(x,main=expression(paste("4. ",bar(x)," = ",bquote(.(meanx)))))

It's either bquote() or expression(). Do not mix them unless you  
understand why they shouldn't be mixed.  Should be:

hist(x,main=bquote(4.~~bar(x)==.(meanx) ) )


> #chart 5:
> hist(x,main=expression(paste("5. ",bar(x)," =
> ",substitute(meanx,list(meanx=meanx)))))
> #chart 6:
> hist(x,main=bquote(paste("6. ",expression(bar(x))," = ",.(meanx))))

This displays on my machine. (with "expression(.)" where the dot is a  
correctly drawn x-bar
)
> #chart 7:
> hist(x,main=paste("7. ",expression(bar(x))," =
> ",substitute(meanx,list(meanx=meanx))))
> #chart 8:
> hist(x,main=bquote("Heart Attack (" * bar(X)==.(meanx) *")"))
>
That displays properly on my machine.


-- 
David.
>
>
> what i want to do is to show a title that says X Bar = mean, where  
> Bar X is
> the mathematical notation for the mean X with a bar on top and mean  
> is the
> mean value
>
> the first 2 charts on top work using bquote() or substitute(), but  
> they
> write the word 'mean' instead of X bar. The third one shows the X  
> Bar with
> no value (it was a first attempt and I never included the value)
>
> charts 4 and 5 and 6 attempt to incorporate either bquote() or  
> substitute
> into #3 to make it work, in both cases it seems the bquote or the  
> substitute
> is not properly being evaluated. In 6 and 7 specifically I put the
> expression inside the bquote or paste to see if reversing would work,
> finally 8 is the one suggested with slight modifications to replace  
> round
> and mean for the variable with that result. (the output I get is  
> attached)
>
> I am hoping that someone can help me understand what I am doing  
> wrong, and I
> guess how the nested functions are being evaluated. I am learning to  
> use R,
> and while I may really never need to use math notation (in fact in  
> my work I
> may confuse people more with X bar than using mean), I believe there  
> is
> value to learning how to use the functions, I can see how they would  
> be
> useful for writing code.functions that write code so one can  
> dynamically
> solve problems. I have done that in SAS and I may do it in R
>
> I appreciate anyones help. Sorry for the long post, but I wanted to  
> show my
> process to solve and learn what is going on
>
> Mario
>
>
> sample.png <http://r.789695.n4.nabble.com/file/n4650645/sample.png>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/characters-mathematical-expressions-and-computed-values-tp4645916p4650645.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list