Control number of decimal places on xtable output in R
You should use the digits
parameter from xtable
function correctly.
table1 <- xtable(t3,caption="Table showing the Mean discharge
and mean gage height on each year on each month",digits=c(0,0,0,3,4))
Each element of that vector represents the number of decimal fields in each column (including the first column with row.names).
xtable R - can't get rid of decimal places
I use
summtab <- xtable(summdf, caption = "Number of obs by some other variable")
names(summtab)<-c("Categorical var", "n obs", "Per cent")
digits(summtab)[3] <- 0
print(summtab, include.rownames = FALSE, booktabs = TRUE, sanitize.text.function = identity)
for making nice tables of dataframes, where [3] counter-intuitively indexes the second column of the dataframe.
The xtable package creating decimal places from nowhere?
Try
xtable( a, digits = c( 0, 0, 2, 2, 2, 2, 2, 0 ) )
% latex table generated in R 3.2.1 by xtable 1.7-4 package
% Thu Aug 6 08:36:53 2015
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrrr}
\hline
& Month & alpha & beta & cov\_td & Q\_BAS & T\_BAS & volume \\
\hline
1 & January 2008 & 0.18 & 0.23 & -0.49 & 0.31 & 0.23 & 70533 \\
2 & February & 0.22 & 0.25 & -0.38 & 0.33 & 0.25 & 48367 \\
3 & March & 0.41 & 0.05 & -0.38 & 0.35 & 0.25 & 70584 \\
4 & April & 0.37 & 0.15 & -0.30 & 0.35 & 0.25 & 46666 \\
5 & May & 0.27 & 0.15 & -0.45 & 0.34 & 0.24 & 72829 \\
6 & June & 0.72 & -0.22 & -0.28 & 0.40 & 0.28 & 60437 \\
\hline
\end{tabular}
\end{table}
As described in the xtable
documentation:
digits
Numeric vector of length equal to one (in which case it will be replicated as
necessary) or to the number of columns of the resulting table or matrix of thes ame size as the resulting table indicating the number of digits to display in the corresponding columns. Since the row names are printed in the first column, the length of the vector digits or the number of columns of the matrix digits is one greater than ncol(x) if x is a data.frame. Default depends of class of x.
If values of digits are negative, the corresponding values of x are displayed in scientific format with abs(digits) digits.
Transpose xtable while formatting number of digits
One problem is that your 'digits' argument is 12 items long. Taking off the leading "0" allows warnings to be reduced:
df <- as.data.frame(matrix(rnorm(22), nrow=2))
print(xtable(t( mapply("format", df, digits=c(0,0,1,0,1,2,0,1,0,0,2)))))
% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Sat Feb 8 11:37:06 2014
\begin{table}[ht]
\centering
\begin{tabular}{rll}
\hline
& 1 & 2 \\
\hline
V1 & 0 & 0 \\
V2 & 0 & 1 \\
V3 & 2.1 & -0.5 \\
V4 & -2 & 1 \\
V5 & -0.7 & -0.7 \\
V6 & 1.03 & -0.28 \\
V7 & -1 & 0 \\
V8 & -0.139 & 0.006 \\
V9 & 0 & -0 \\
V10 & 1 & -0 \\
V11 & 0.33 & 1.10 \\
\hline
\end{tabular}
\end{table}
The transpose functions for matrices and data.frames seem to enforce column uniformity of digit width (or perhaps it is their print methods?
Here's a really kludgy effort:
capture.output(apply(cbind(t(df), digits), 1, function(x)
cat( c(round( x[1:2], x[3]) ,"\n") ) ) )
[1] "0 0 " "0 1 " "2.1 -0.5 " "-2 1 " "-0.7 -0.7 "
[6] "1.03 -0.28 " "-1 0 " "-0.1 0 " "0 0 " "1 0 "
[11] "0.33 1.1 " "NULL"
Adjust number of decimal places in a dataset
Test %>%
mutate(sum = rowSums(across(3:last_col()), na.rm = TRUE),
across(where(is.numeric), ~sprintf("US $%.2f", .x)))
date2 Category coef1 coef2 sum
1 2021-06-30 FDE US $445.23 US $8.32 US $453.56
2 2021-06-30 ABC US $1.31 US $3.34 US $4.66
3 2021-07-01 FDE US $6.32 US $1.32 US $7.65
4 2021-07-02 ABC US $1.23 US $6.32 US $7.56
Limit decimal places in variable in R
You can use round() option or digit.
The following code shows exactly two decimal places for the number using round()
format(round(x, 2), nsmall = 2)
For example:
> format(round(1.20, 2), nsmall = 2) [1] "1.20"
> format(round(1, 2), nsmall = 2) [1] "1.00"
> format(round(1.1234, 2), nsmall = 2) [1] "1.12"
You can format a number, say x, up to decimal places as your wish. Here x is a number with big decimal places , you can format decimal places as your wish. Such that we wish to take up to 8 decimal places of this number.
x<-c(1111111234.6547389758965789345)
y<-formatC(x,digits=8,format="f")
[1] "1111111234.65473890"
preserve decimal places and vertically align decimals in output table
A few alternatives, depending on your needs:
Use print.data.frame
This benefits from (or is encumbered by, depending on your perspective) options("digits")
and perhaps others.
out <- paste(capture.output(print(df, row.names=F)), collapse = "\n")
writeLines(out, "df.dat")
File contents:
col1 col2
a 0.60
a 1234.55
a 678.90
a -999.00
Manual stringification/alignment
df$col2 <- sprintf("%0.02f", df$col2)
df$col2 <- sprintf(paste0("%0", max(nchar(df$col2)), "s"), df$col2)
write.table(df, "df.dat", quote = FALSE, row.names = FALSE)
File contents (notice column-alignment is not being handled perfectly):
col1 col2
a 0.60
a 1234.55
a 678.90
a -999.00
knitr::kable
If this is for looks, then perhaps one of the kable
variants will work:
knitr::kable(df)
# |col1 | col2|
# |:----|-------:|
# |a | 0.60|
# |a | 1234.55|
# |a | 678.90|
# |a | -999.00|
Related Topics
How to Create Thiessen Polygons from Points Using R Packages
Using Lapply with Changing Arguments
Rename a Sequence of Variable Names in Data Frame
Why Is Expand.Grid Faster Than Data.Table 's Cj
How to Read a Password Protected Excel File into R
How to Insert (Add) a Row to a SQLite Db Table Using Dplyr Package
Replace Character at Certain Location Within String
Arrange N Ggplots into Lower Triangle Matrix Shape
Using Trycatch and Rvest to Deal with 404 and Other Crawling Errors
Change Text Color for Cells Using Tablegrob
How to Compute Correlations Between All Columns in R and Detect Highly Correlated Variables
How to Change Font Family in a Legend in an R-Plot
Grouped Barplot with Cut Y Axis
Create Plots Based on Radio Button Selection R Shiny
How to Do a Data.Table Merge Operation
How to Pass Input Variable to SQL Statement in R Shiny