r - 在 xtable 中设置不同的数字
问题描述
我有一个像 mtcars 这样的数据集,我想使用 xtable 和数字 = 2 打印它,但是carb
我想在列中看到没有数字的值
xtable(head(mtcars), digits =2)
预期结果,请看最后一栏
% latex table generated in R 4.0.2 by xtable 1.8-4 package
% Mon Oct 12 10:10:25 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrr}
\hline
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4\\
Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\
Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1\\
Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2\\
Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\
\hline
\end{tabular}
\end{table}
提前谢谢了
解决方案
您可以为每列定义数字。您的表格有 12 列。所以前 11 列有 2 位数字,第 12 列有 0 位数字。
xtable::xtable(head(mtcars), digits = c(rep(2, 11), 0))
结果
% latex table generated in R 3.6.3 by xtable 1.8-4 package
% Mon Oct 12 10:25:04 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrr}
\hline
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4 \\
Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\
Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1 \\
Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2 \\
Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\
\hline
\end{tabular}
\end{table}
推荐阅读
- python - python中的轨迹交叉点
- python - 如何防止“hyperopt”将失败的模型结果整合到参数更新中?
- c++ - 使用 StretchDIBits 复制 PNG(带 alpha)
- python - 熊猫矢量化操作中的多个布尔条件
- docker - 从 compose 容器访问 dockerfile 的卷
- node.js - 如何使用 Pino.js 渲染新行
- ios - 如何在 Firestore 中存储对象数组
- python - 返回语句不返回电子邮件的值
- git - IntelliJ IDEA 无法识别我的项目在 git 中,分支名称为“N/A”
- python - 从列表列表中删除字符串