gpt4 book ai didi

r - 如何使用 ascii、html 或 markdown 格式在 R 中打印表格?

转载 作者:行者123 更新时间:2023-12-03 13:43:28 25 4
gpt4 key购买 nike

library(xtable)
library(rattle)
set.seed(42)
obs <- sample(1:nrow(weatherAUS), 5)
vars <- 2:7
xtable(weatherAUS[obs, vars])

我用代码得到以下输出,为什么我不能得到格式化的表?
% latex table generated in R 2.15.1 by xtable 1.7-1 package  
% Sat Apr 6 17:02:37 2013
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrr}
\hline
& Location & MinTemp & MaxTemp & Rainfall & Evaporation & Sunshine \\
\hline
60992 & Hobart & 5.60 & 13.00 & 7.60 & 1.60 & 3.10 \\
62476 & Launceston & 7.40 & 13.50 & 8.80 & & \\
19077 & Williamtown & 18.30 & 29.10 & 3.20 & 1.00 & 7.00 \\
55366 & PerthAirport & 9.80 & 21.90 & 0.00 & 3.60 & 9.80 \\
42784 & GoldCoast & 23.40 & 30.40 & 0.00 & & \\
\hline
\end{tabular}
\end{table}

最佳答案

您从 xtable 返回的内容|格式很好,但由于它采用 LaTeX 语法,因此值得运行像 pdflatex 这样的 LaTeX 编译器。 .这将返回这样的 pdf 文档:

compiled pdf file

如果你想在 R 控制台中有一个格式化的表格,那么一个相当人类可读的标准版本 print.data.frame ,你可以试试 ascii或我的 pander包裹。例子:

  • 基本款ascii称呼:
    > library(ascii)
    > ascii(weatherAUS[obs, vars])
    |===================================================================================
    1.1+| h| Location h| MinTemp h| MaxTemp h| Rainfall h| Evaporation h| Sunshine
    | 60992 | Hobart | 5.60 | 13.00 | 7.60 | 1.60 | 3.10
    | 62476 | Launceston | 7.40 | 13.50 | 8.80 | |
    | 19077 | Williamtown | 18.30 | 29.10 | 3.20 | 1.00 | 7.00
    | 55366 | PerthAirport | 9.80 | 21.90 | 0.00 | 3.60 | 9.80
    | 42784 | GoldCoast | 23.40 | 30.40 | 0.00 | |
    |===================================================================================
  • 调用 ascii返回表格,例如重构文本格式:
    > print(ascii(weatherAUS[obs, vars]), type = "rest")

    +-------+--------------+---------+---------+----------+-------------+----------+
    | | Location | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine |
    +=======+==============+=========+=========+==========+=============+==========+
    | 60992 | Hobart | 5.60 | 13.00 | 7.60 | 1.60 | 3.10 |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 62476 | Launceston | 7.40 | 13.50 | 8.80 | | |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 19077 | Williamtown | 18.30 | 29.10 | 3.20 | 1.00 | 7.00 |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 55366 | PerthAirport | 9.80 | 21.90 | 0.00 | 3.60 | 9.80 |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 42784 | GoldCoast | 23.40 | 30.40 | 0.00 | | |
    +-------+--------------+---------+---------+----------+-------------+----------+
  • 使用 pander以不同的 Markdown 格式返回表格:
    > library(pander)
    > panderOptions('table.split.table', Inf)
    > pander(weatherAUS[obs, vars])

    --------------------------------------------------------------------------------
    &nbsp; Location MinTemp MaxTemp Rainfall Evaporation Sunshine
    ----------- ------------ --------- --------- ---------- ------------- ----------
    **60992** Hobart 5.6 13.0 7.6 1.6 3.1

    **62476** Launceston 7.4 13.5 8.8

    **19077** Williamtown 18.3 29.1 3.2 1.0 7.0

    **55366** PerthAirport 9.8 21.9 0.0 3.6 9.8

    **42784** GoldCoast 23.4 30.4 0.0
    --------------------------------------------------------------------------------
  • 或在 grid格式:
    > pandoc.table(weatherAUS[obs, vars], style = 'grid')

    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    | &nbsp; | Location | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine |
    +=============+==============+===========+===========+============+===============+============+
    | **60992** | Hobart | 5.6 | 13.0 | 7.6 | 1.6 | 3.1 |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    | **62476** | Launceston | 7.4 | 13.5 | 8.8 | | |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    | **19077** | Williamtown | 18.3 | 29.1 | 3.2 | 1.0 | 7.0 |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    | **55366** | PerthAirport | 9.8 | 21.9 | 0.0 | 3.6 | 9.8 |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    | **42784** | GoldCoast | 23.4 | 30.4 | 0.0 | | |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
  • 更多 simple格式:
    > pandoc.table(weatherAUS[obs, vars], style = 'simple')

    &nbsp; Location MinTemp MaxTemp Rainfall Evaporation Sunshine
    ----------- ------------ --------- --------- ---------- ------------- ----------
    **60992** Hobart 5.6 13.0 7.6 1.6 3.1
    **62476** Launceston 7.4 13.5 8.8
    **19077** Williamtown 18.3 29.1 3.2 1.0 7.0
    **55366** PerthAirport 9.8 21.9 0.0 3.6 9.8
    **42784** GoldCoast 23.4 30.4 0.0
  • 或者在 PHPMarkdown Extra/piped 语法中与 knitr 一起使用:
    > pandoc.table(weatherAUS[obs, vars], style = 'rmarkdown')

    | &nbsp; | Location | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine |
    |:-----------:|:------------:|:---------:|:---------:|:----------:|:-------------:|:----------:|
    | **60992** | Hobart | 5.6 | 13.0 | 7.6 | 1.6 | 3.1 |
    | **62476** | Launceston | 7.4 | 13.5 | 8.8 | | |
    | **19077** | Williamtown | 18.3 | 29.1 | 3.2 | 1.0 | 7.0 |
    | **55366** | PerthAirport | 9.8 | 21.9 | 0.0 | 3.6 | 9.8 |
    | **42784** | GoldCoast | 23.4 | 30.4 | 0.0 | | |
  • 关于r - 如何使用 ascii、html 或 markdown 格式在 R 中打印表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849043/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com