[R] xtable use plus minus

arun smartpink111 at yahoo.com
Fri Sep 13 04:54:02 CEST 2013


HI,
Sorry, there was a mistake as I didn't properly looked at the output you wanted.

a_tableNew<- data.matrix(matrix(0,3,3))

a_tableNew[2:3,2:3]<-paste0(sprintf("%.2f",as.numeric( a_table[,seq(2,5,2)][grep("\\d+",a_table[,seq(2,5,2)])])),"$\\pm$",sprintf("%.2f",as.numeric( a_table[,seq(3,5,2)][grep("\\d+",a_table[,seq(2,5,2)])])))
a_tableNew[grep("\\b0\\b",a_tableNew)]<- a_table[!grepl("\\d+",a_table)][!grepl("error",a_table[!grepl("\\d+",a_table)])]
 a_tableNew
#     [,1]      [,2]              [,3]             
#[1,] "Fruits"  "Adam"            "steve"          
#[2,] "apples"  "17.10$\\pm$2.22" "3.20$\\pm$1.10" 
#[3,] "oranges" "3.10$\\pm$2.55"  "18.10$\\pm$3.20"


print(xtable(a_tableNew),sanitize.text.function=identity)

#If you wanted the column names as "Fruits", "Adam" etc,

colnames(a_tableNew)<- a_tableNew[1,]
 a_tableNew<- a_tableNew[-1,]


print(xtable(a_tableNew),sanitize.text.function=identity)
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Thu Sep 12 22:42:58 2013
\begin{table}[ht]
\centering
\begin{tabular}{rlll}
  \hline
 & Fruits & Adam & steve \\ 
  \hline
1 & apples & 17.10$\pm$2.22 & 3.20$\pm$1.10 \\ 
  2 & oranges & 3.10$\pm$2.55 & 18.10$\pm$3.20 \\ 
   \hline
\end{tabular}
\end{table}


A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, September 12, 2013 10:21 PM
Subject: Re: [R] xtable use plus minus

Hi,
Also:


a_table[grep("\\d+",a_table)]<-paste0(sprintf("%.2f",as.numeric(a_table[grep("\\d+",a_table)])),"$\\pm$") # 2 d.p.
print(xtable(a_table),sanitize.text.function=identity)


A.K.

----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, September 12, 2013 9:29 PM
Subject: Re: [R] xtable use plus minus



Hi,
Try:
a_table[grep("\\d+",a_table)]<- paste0(a_table[grep("\\d+",a_table)],"$\\pm$")
library(xtable)
 print(xtable(a_table),sanitize.text.function=identity)
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Thu Sep 12 21:26:45 2013
\begin{table}[ht]
\centering
\begin{tabular}{rlllll}
  \hline
 & 1 & 2 & 3 & 4 & 5 \\ 
  \hline
1 & Fruits & Adam & errorA & steve & errorS \\ 
  2 & apples & 17.1$\pm$ & 2.22$\pm$ & 3.2$\pm$ & 1.1$\pm$ \\ 
  3 & oranges & 3.1$\pm$ & 2.55$\pm$ & 18.1$\pm$ & 3.2$\pm$ \\ 
   \hline
\end{tabular}
\end{table}

A.K.



----- Original Message -----
From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Thursday, September 12, 2013 8:46 PM
Subject: [R] xtable use plus minus

I am using a similar dataset to the following:

a= c("Fruits", "Adam","errorA", "steve", "errorS",
     "apples", 17.1,2.22, 3.2,1.1,
     "oranges", 3.1,2.55, 18.1,3.2 )
a_table=data.matrix(t(matrix(a,nrow=5)))

I would like to plus minus every second column starting from errorA (using
xtable/ hmisc)

example output (ignoring decimals):

Fruits && Adam && Steve \\
Apples && 17\pm 2 && 3 \pm 1 \\
Oranges && 3\pm 2 && 18 \pm 3\\

Additionally is there any way I can have just to 2 d.p.?

Thanks,
Sachin

    [[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.




More information about the R-help mailing list