digitsbyrow: digitsbyrow a helper function for knitr, to specify formats...

View source: R/rutils.R

digitsbyrowR Documentation

digitsbyrow a helper function for knitr, to specify formats by row

Description

digitsbyrow is a solution obtained from StackOverFlow, suggested by Tim Bainbridge in 11/12/19. knitr formats table columns as a whole, which can be a problem if one wants to mix integers with real numbers in the same columns. This first transposes the data.frame/matrix being printed, fixes the formats, and then transposes it back. In knitr one then needs to use the align argument to fix the alignment. In may version I have conserved both rownames and colnames for both data.frames and matrices (the original only did so for data.frames but I often print matrices). digitsbyrow converts all entries to character so knitr becomes necessary for printing.

Usage

digitsbyrow(df, digits)

Arguments

df

the data.frame or matrix to be printed by knitr

digits

a vector of the digits wanted for each row of the df or matrix

Value

a formatted data.frame or matrix depending on input

Examples

x <- matrix(c(rnorm(5,mean=5,sd=1),seq(1,10,1)),nrow=3,ncol=5,byrow=TRUE,
            dimnames=list(1:3,1:5))
digitsbyrow(x, c(3,0,0))
# needs knitr to use kable
# kable(digitsbyrow(x, c(3,0,0)),align='r',row.names=TRUE)

haddonm/codeutils documentation built on April 15, 2024, 1:02 p.m.