根据数据框列类型R对齐xtable列

时间:2017-11-30 14:46:34

标签: r dataframe knitr xtable

我在xtable中使用knitr来创建表格,目前我将所有xtable列对齐,因为它很容易且可重复。

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

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为情况已经如此:来自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