I am trying to construct an R matrix/data frame with special characters in it. For example, one cell would contain "2\±5\³"
(ie "2±5³").
I need to have Latin lowercase alphabets in the superscript, instead of the number superscript and I was only able to find the Unicode for few alphabets as superscripts .
I am familiar with all the special symbols within the plot functions (eg for labeling axis, titles etc.) but I need the "superscripting" within a matrix.
EDIT: I wanted to do something like this, with only one exception, instead of the 3 in the supescript in 3.5\±2\³
, I want to have latin alphabets.
require(rtf)
rtf<-RTF("./Doc/test_addTable.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1))
A = data.frame(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2,0.2,0.2, 0.4))
A[1,2] = "3.5\u00B12\u00B3"
addTable(rtf,A,font.size=10,row.names=FALSE,NA.string="-",col.widths=rep(1,4))
done(rtf)
Thank you for your time.
In R there is a system that is like mini-LaTeX but is called "plotmath". It is driven off expression objects and is invoked when an expression hits a text=plotting value. It doesn't really make much sense to have such a matrix if you are not going to plot it, so I will illustrate how to raise character values as superscripts.
?plotmath
plot(1,1)
text( x=c(.8), y=c(1.2), substitute( 2%+-%5^x, list(x="A") ))
That is a bit more complicate than might be needed for a single instance but that is designed to increase generality. You could also do:
text( x=c(.8), y=c(.8), expression(2%+-%5^B) )
If you just want a matrix of plotmath (in character form):
mat <- matrix( c( '2%+-%5^B' , '2%+-%5^C',
'2%+-%5^D' , '2%+-%5^B'), 2, 2)
mat
You cannot (or at least I cannot) really construct a true R expression object as a matrix:
mat <- matrix( expression(2%+-%5^B , 2%+-%5^C,
2%+-%5^D , 2%+-%5^B), 2, 2)
mat
#expression(2 %+-% 5^B, 2 %+-% 5^C, 2 %+-% 5^D, 2 %+-% 5^B)
> str(mat)
length 4 expression(2 %+-% 5^B, 2 %+-% 5^C, 2 %+-% 5^D) ...
- attr(*, "dim")= int [1:2] 2 2
> mat[1,1]
Error in mat[1, 1] : matrix subscripting not handled for this type
> mat[1]
expression(2 %+-% 5^B)
> mat[2]
expression(2 %+-% 5^C)
> mat[3]
expression(2 %+-% 5^D)
I did manage to get the matrix function to attach dimensions to an expression object but neither print
nor [
would honor that dimension. I remain unclear as to the goal. In the rtf
package I see the addTable method and I'm wondering if you you really want to have an object that will be printed by an rtf-aware application in "matrix" or tabular form?
Hah.... I think I have it:
mat <- matrix( c( '2\\+/-5 {\\up3B}' , '2%+-%5 \\up5C',
'2%+-%5\\up3B' , '2%+-%5\\up4H'), 2, 2)
mat
# [,1] [,2]
#[1,] "2\\+/-5 {\\up3B}" "2%+-%5\\up3B"
#[2,] "2%+-%5 \\up5C" "2%+-%5\\up4H"
rtf<-RTF("test_addTable.doc",width=8.5,height=11,
font.size=10,omi=c(1,1,1,1))
addTable(rtf, mat)
done(rtf)
Furthermore I think I fingered out the plusminus sign. Try:
"2\\u177BD5 {\\up3B}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.