【问题标题】:Align xtable columns based on dataframe column type R根据数据框列类型 R 对齐 xtable 列
【发布时间】:2018-05-14 12:59:02
【问题描述】:

我在knitr 中使用xtable 创建表格,目前我左对齐所有xtable 列,因为它简单且可重复。

但是,我想知道是否有办法根据数据框列中的数据类型(num、chr、int 等)对齐 xtable 列,以便我可以右对齐数字和左对齐字符。

谢谢!

【问题讨论】:

    标签: r dataframe knitr xtable


    【解决方案1】:

    我认为已经是这样了:来自 xtable 示例页面

     precip
                 Mobile              Juneau             Phoenix         Little Rock         Los Angeles          Sacramento       San Francisco 
                   67.0                54.7                 7.0                48.5                14.0                17.2                20.7 
                 Denver            Hartford          Wilmington          Washington        Jacksonville               Miami             Atlanta 
                   13.0                43.4                40.2                38.9                54.5                59.8                48.3 
               Honolulu               Boise             Chicago              Peoria        Indianapolis          Des Moines             Wichita 
                   22.9                11.5                34.4                35.1                38.7                30.8                30.6 
             Louisville         New Orleans            Portland           Baltimore              Boston             Detroit    Sault Ste. Marie 
                   43.1                56.8                40.8                41.8                42.5                31.0                31.7 
                 Duluth Minneapolis/St Paul             Jackson         Kansas City            St Louis         Great Falls               Omaha 
                   30.2                25.9                49.2                37.0                35.9                15.0                30.2 
                   Reno             Concord       Atlantic City         Albuquerque              Albany             Buffalo            New York 
                    7.2                36.2                45.5                 7.8                33.4                36.1                40.2 
              Charlotte             Raleigh             Bismark          Cincinnati           Cleveland            Columbus       Oklahoma City 
                   42.7                42.5                16.2                39.0                35.0                37.0                31.4 
               Portland        Philadelphia           Pittsburg          Providence            Columbia         Sioux Falls             Memphis 
                   37.6                39.9                36.2                42.8                46.4                24.7                49.1 
              Nashville              Dallas             El Paso             Houston      Salt Lake City          Burlington             Norfolk 
                   46.0                35.9                 7.8                48.2                15.2                32.5                44.7 
               Richmond      Seattle Tacoma             Spokane          Charleston           Milwaukee            Cheyenne            San Juan 
                   42.6                38.8                17.4                40.8                29.1                14.6                59.2 
    > xalign(names(precip))# a character vector
    [1] "l"
    > xalign(precip)# a numeric
    [1] "r
    

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2014-04-23
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2016-12-09
      • 1970-01-01
      • 2012-11-15
      相关资源
      最近更新 更多