[R] xtable does not print out units of a variable
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Jul 19 04:25:34 CEST 2018
On Wed, 18 Jul 2018, Shawn Way wrote:
> I have a dataframe that contains units using the units package.
> Unfortunately, I really need the units for reporting. I'm assuming
> that's because the data is in a class units and xtable doesn't know what
> to do with this.
If you want a bug or feature in a CONTRIBUTED PACKAGE, then you need to
communicate with the maintainer:
maintainer( "xtable" )
Do keep in mind that they almost always volunteer their time, so be
patient, and consider figuring out what code changes they need to make so
it will work.
More below.
> The following is a MWE:
>
> library(xtable)
> library(units)
> data <- data.frame(x=c(as_units(12,"ft")))
> xtable(data)
>
> % latex table generated in R 3.5.1 by xtable 1.8-2 package
> % Wed Jul 18 17:31:44 2018
> \begin{table}[ht]
> \centering
> \begin{tabular}{rr}
> \hline
> & x \\
> \hline
> 1 & 12.00 \\
> \hline
> \end{tabular}
> \end{table}
>
> What I'm looking for is the line
>
> 1 & 12.00 \\
>
> to be
>
> 1 & 12.00 $ft$\\
>
> Can someone point me in the correct direction to make this happen?
> Since units are used extensively in engineering calculations, being able
> to handle this class would be extremely beneficial to engineers that are
> using R with knitr to generate engineering documents.
>
> Shawn Way
I do want to emphasize that R focuses on consistency among elements within
columns, not rows, so putting the units into the body of the table is kind
of visually redundant in most cases. Consider:
####################
library(xtable)
library(units)
#> udunits system database from /usr/share/xml/udunits
data <- data.frame(x=c(as_units(c(12,13),"ft")))
datax <- xtable(data)
names(datax) <- paste0( names(datax)[1]
, " ($"
, deparse_unit( datax[[1]] )
, "$)"
)
datax
#> % latex table generated in R 3.4.4 by xtable 1.8-2 package
#> % Wed Jul 18 19:13:29 2018
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{rr}
#> \hline
#> & x (\$ft\$) \\
#> \hline
#> 1 & 12.00 \\
#> 2 & 13.00 \\
#> \hline
#> \end{tabular}
#> \end{table}
#' Created on 2018-07-18 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
####################
If you have some kind of summary table with different units on each row,
then you will probably arrive at that information a single-row, many
column data frame. I usually transpose this into a three-column data frame
with a description column, a value column, and a units column. I don't use
the units package so have never tried to adapt it to that process.
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil using dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
More information about the R-help
mailing list