Search code examples
rdataframeknitrxtable

Align xtable columns based on dataframe column type R


I am using xtable in knitr to create tables and currently I left align all xtable columns because it is easy and repeatable.

However, I am wondering if there is a way to align the xtable columns depending on the type of data in the dataframe column (num, chr, int, etc...) so that I can right align numbers and left align characters.

Thanks!


Solution

  • I think it's already the case : from xtable example page

     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