How to display xtable values in scientific form in R?

R ProgrammingServer Side ProgrammingProgramming

The xtable function of xtable package creates a Latex table. We can use digits argument with negative sign to convert the values in the original table into scientific form. For example, if we have a data frame defined as df then we can read it with xtable as xtable(df,digits=-10).

Loading xtable package −

library(xtable)

Example1

data1<−xtable(CO2[1:20,])
data1
% latex table generated in R 4.0.2 by xtable 1.8−4 package
% Tue Oct 20 14:36:00 2020
\begin{table}[ht]
\centering
\begin{tabular}{rlllrr}
\hline
& Plant & Type & Treatment & conc & uptake \\
\hline
1 & Qn1 & Quebec & nonchilled & 95.00 & 16.00 \\
2 & Qn1 & Quebec & nonchilled & 175.00 & 30.40 \\
3 & Qn1 & Quebec & nonchilled & 250.00 & 34.80 \\
4 & Qn1 & Quebec & nonchilled & 350.00 & 37.20 \\
5 & Qn1 & Quebec & nonchilled & 500.00 & 35.30 \\
6 & Qn1 & Quebec & nonchilled & 675.00 & 39.20 \\
7 & Qn1 & Quebec & nonchilled & 1000.00 & 39.70 \\
8 & Qn2 & Quebec & nonchilled & 95.00 & 13.60 \\
9 & Qn2 & Quebec & nonchilled & 175.00 & 27.30 \\
10 & Qn2 & Quebec & nonchilled & 250.00 & 37.10 \\
11 & Qn2 & Quebec & nonchilled & 350.00 & 41.80 \\
12 & Qn2 & Quebec & nonchilled & 500.00 & 40.60 \\
13 & Qn2 & Quebec & nonchilled & 675.00 & 41.40 \\
14 & Qn2 & Quebec & nonchilled & 1000.00 & 44.30 \\
15 & Qn3 & Quebec & nonchilled & 95.00 & 16.20 \\
16 & Qn3 & Quebec & nonchilled & 175.00 & 32.40 \\
17 & Qn3 & Quebec & nonchilled & 250.00 & 40.30 \\
18 & Qn3 & Quebec & nonchilled & 350.00 & 42.10 \\
19 & Qn3 & Quebec & nonchilled & 500.00 & 42.90 \\
20 & Qn3 & Quebec & nonchilled & 675.00 & 43.90 \\
\hline
\end{tabular}
\end{table}
data_table1<−xtable(data1[1:20,],digits=−10)
data_table1
% latex table generated in R 4.0.2 by xtable 1.8−4 package
% Tue Oct 20 14:36:25 2020
\begin{table}[ht]
\centering
\begin{tabular}{rlllrr}
\hline
& Plant & Type & Treatment & conc & uptake \\
\hline
1 & Qn1 & Quebec & nonchilled & 9.5000000000E+01 & 1.6000000000E+01 \\
2 & Qn1 & Quebec & nonchilled & 1.7500000000E+02 & 3.0400000000E+01 \\
3 & Qn1 & Quebec & nonchilled & 2.5000000000E+02 & 3.4800000000E+01 \\
4 & Qn1 & Quebec & nonchilled & 3.5000000000E+02 & 3.7200000000E+01 \\
5 & Qn1 & Quebec & nonchilled & 5.0000000000E+02 & 3.5300000000E+01 \\
6 & Qn1 & Quebec & nonchilled & 6.7500000000E+02 & 3.9200000000E+01 \\
7 & Qn1 & Quebec & nonchilled & 1.0000000000E+03 & 3.9700000000E+01 \\
8 & Qn2 & Quebec & nonchilled & 9.5000000000E+01 & 1.3600000000E+01 \\
9 & Qn2 & Quebec & nonchilled & 1.7500000000E+02 & 2.7300000000E+01 \\
10 & Qn2 & Quebec & nonchilled & 2.5000000000E+02 & 3.7100000000E+01 \\
11 & Qn2 & Quebec & nonchilled & 3.5000000000E+02 & 4.1800000000E+01 \\
12 & Qn2 & Quebec & nonchilled & 5.0000000000E+02 & 4.0600000000E+01 \\
13 & Qn2 & Quebec & nonchilled & 6.7500000000E+02 & 4.1400000000E+01 \\
14 & Qn2 & Quebec & nonchilled & 1.0000000000E+03 & 4.4300000000E+01 \\
15 & Qn3 & Quebec & nonchilled & 9.5000000000E+01 & 1.6200000000E+01 \\
16 & Qn3 & Quebec & nonchilled & 1.7500000000E+02 & 3.2400000000E+01 \\
17 & Qn3 & Quebec & nonchilled & 2.5000000000E+02 & 4.0300000000E+01 \\
18 & Qn3 & Quebec & nonchilled & 3.5000000000E+02 & 4.2100000000E+01 \\
19 & Qn3 & Quebec & nonchilled & 5.0000000000E+02 & 4.2900000000E+01 \\
20 & Qn3 & Quebec & nonchilled & 6.7500000000E+02 & 4.3900000000E+01 \\
\hline
\end{tabular}
\end{table}

Example2

data2<−xtable(sleep[1:20,])
data2
% latex table generated in R 4.0.2 by xtable 1.8-4 package
% Tue Oct 20 14:37:14 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrll}
\hline
& extra & group & ID \\
\hline
1 & 0.70 & 1 & 1 \\
2 & −1.60 & 1 & 2 \\
3 & −0.20 & 1 & 3 \\
4 & −1.20 & 1 & 4 \\
5 & −0.10 & 1 & 5 \\
6 & 3.40 & 1 & 6 \\
7 & 3.70 & 1 & 7 \\
8 & 0.80 & 1 & 8 \\
9 & 0.00 & 1 & 9 \\
10 & 2.00 & 1 & 10 \\
11 & 1.90 & 2 & 1 \\
12 & 0.80 & 2 & 2 \\
13 & 1.10 & 2 & 3 \\
14 & 0.10 & 2 & 4 \\
15 & −0.10 & 2 & 5 \\
16 & 4.40 & 2 & 6 \\
17 & 5.50 & 2 & 7 \\
18 & 1.60 & 2 & 8 \\
19 & 4.60 & 2 & 9 \\
20 & 3.40 & 2 & 10 \\
\hline
\end{tabular}
\end{table}
data_table2<−xtable(data2[1:20,],digits=−10)
data_table2
% latex table generated in R 4.0.2 by xtable 1.8−4 package
% Tue Oct 20 14:37:29 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrll}
\hline
& extra & group & ID \\
\hline
1 & 7.0000000000E−01 & 1 & 1 \\
2 & −1.6000000000E+00 & 1 & 2 \\
3 & −2.0000000000E−01 & 1 & 3 \\
4 & −1.2000000000E+00 & 1 & 4 \\
5 & −1.0000000000E−01 & 1 & 5 \\
6 & 3.4000000000E+00 & 1 & 6 \\
7 & 3.7000000000E+00 & 1 & 7 \\
8 & 8.0000000000E−01 & 1 & 8 \\
9 & 0.0000000000E+00 & 1 & 9 \\
10 & 2.0000000000E+00 & 1 & 10 \\
11 & 1.9000000000E+00 & 2 & 1 \\
12 & 8.0000000000E−01 & 2 & 2 \\
13 & 1.1000000000E+00 & 2 & 3 \\
14 & 1.0000000000E−01 & 2 & 4 \\
15 & −1.0000000000E−01 & 2 & 5 \\
16 & 4.4000000000E+00 & 2 & 6 \\
17 & 5.5000000000E+00 & 2 & 7 \\
18 & 1.6000000000E+00 & 2 & 8 \\
19 & 4.6000000000E+00 & 2 & 9 \\
20 & 3.4000000000E+00 & 2 & 10 \\
\hline
\end{tabular}
\end{table}

Example3

data3<−xtable(iris[1:20,])
data3
% latex table generated in R 4.0.2 by xtable 1.8−4 package
% Tue Oct 20 14:38:06 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa \\
2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa \\
3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa \\
4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa \\
5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa \\
6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa \\
7 & 4.60 & 3.40 & 1.40 & 0.30 & setosa \\
8 & 5.00 & 3.40 & 1.50 & 0.20 & setosa \\
9 & 4.40 & 2.90 & 1.40 & 0.20 & setosa \\
10 & 4.90 & 3.10 & 1.50 & 0.10 & setosa \\
11 & 5.40 & 3.70 & 1.50 & 0.20 & setosa \\
12 & 4.80 & 3.40 & 1.60 & 0.20 & setosa \\
13 & 4.80 & 3.00 & 1.40 & 0.10 & setosa \\
14 & 4.30 & 3.00 & 1.10 & 0.10 & setosa \\
15 & 5.80 & 4.00 & 1.20 & 0.20 & setosa \\
16 & 5.70 & 4.40 & 1.50 & 0.40 & setosa \\
17 & 5.40 & 3.90 & 1.30 & 0.40 & setosa \\
18 & 5.10 & 3.50 & 1.40 & 0.30 & setosa \\
19 & 5.70 & 3.80 & 1.70 & 0.30 & setosa \\
20 & 5.10 & 3.80 & 1.50 & 0.30 & setosa \\
\hline
\end{tabular}
\end{table}
data_table3<−xtable(data3[1:20,],digits=−10)
data_table3
% latex table generated in R 4.0.2 by xtable 1.8−4 package
% Tue Oct 20 14:38:19 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5.1000000000E+00 & 3.5000000000E+00 & 1.4000000000E+00 & 2.0000000000E−01 & setosa \\
2 & 4.9000000000E+00 & 3.0000000000E+00 & 1.4000000000E+00 & 2.0000000000E−01 & setosa \\
3 & 4.7000000000E+00 & 3.2000000000E+00 & 1.3000000000E+00 & 2.0000000000E−01 & setosa \\
4 & 4.6000000000E+00 & 3.1000000000E+00 & 1.5000000000E+00 & 2.0000000000E−01 & setosa \\
5 & 5.0000000000E+00 & 3.6000000000E+00 & 1.4000000000E+00 & 2.0000000000E−01 & setosa \\
6 & 5.4000000000E+00 & 3.9000000000E+00 & 1.7000000000E+00 & 4.0000000000E−01 & setosa \\
7 & 4.6000000000E+00 & 3.4000000000E+00 & 1.4000000000E+00 & 3.0000000000E−01 & setosa \\
8 & 5.0000000000E+00 & 3.4000000000E+00 & 1.5000000000E+00 & 2.0000000000E−01 & setosa \\
9 & 4.4000000000E+00 & 2.9000000000E+00 & 1.4000000000E+00 & 2.0000000000E−01 & setosa \\
10 & 4.9000000000E+00 & 3.1000000000E+00 & 1.5000000000E+00 & 1.0000000000E−01 & setosa \\
11 & 5.4000000000E+00 & 3.7000000000E+00 & 1.5000000000E+00 & 2.0000000000E−01 & setosa \\
12 & 4.8000000000E+00 & 3.4000000000E+00 & 1.6000000000E+00 & 2.0000000000E−01 & setosa \\
13 & 4.8000000000E+00 & 3.0000000000E+00 & 1.4000000000E+00 & 1.0000000000E−01 & setosa \\
14 & 4.3000000000E+00 & 3.0000000000E+00 & 1.1000000000E+00 & 1.0000000000E−01 & setosa \\
15 & 5.8000000000E+00 & 4.0000000000E+00 & 1.2000000000E+00 & 2.0000000000E−01 & setosa \\
16 & 5.7000000000E+00 & 4.4000000000E+00 & 1.5000000000E+00 & 4.0000000000E−01 & setosa \\
17 & 5.4000000000E+00 & 3.9000000000E+00 & 1.3000000000E+00 & 4.0000000000E−01 & setosa \\
18 & 5.1000000000E+00 & 3.5000000000E+00 & 1.4000000000E+00 & 3.0000000000E−01 & setosa \\
19 & 5.7000000000E+00 & 3.8000000000E+00 & 1.7000000000E+00 & 3.0000000000E−01 & setosa \\
20 & 5.1000000000E+00 & 3.8000000000E+00 & 1.5000000000E+00 & 3.0000000000E−01 & setosa \\
\hline
\end{tabular}
\end{table}
raja
Published on 06-Nov-2020 14:08:58
Advertisements