Brian Hobbs
2012-Nov-08 16:14 UTC
[R] Formatting digits in a table with mix of numbers and characters
Hello, I am currently trying to find an easy way to take a table with a mix of numbers and characters and format the numbers in the table to be constrained to 4 significant digits. Example: #this is my table creation> table <- matrix(c(12.34567, "--", 10, 12.34567, "--", "NA", "--", 123.45678, "--"), ncol = 3, byrow=TRUE) > > table[,1] [,2] [,3] [1,] "12.34567" "--" "10" [2,] "12.34567" "--" "NA" [3,] "--" "123.45678" "--" #I would like for it to be [,1] [,2] [,3] [1,] "12.35" "--" "10" [2,] "12.35" "--" "NA" [3,] "--" "123.5" "--" I am using the "xtable" package and attempt to use the digits() argument within xtable; however, this did not format the numbers, likely because my table was forced to character values when I created it using matrix(). I am not bothered by the character values in my final xtable result; however... I have two questions: 1) Is there a better way to create my table so that the original numeric values are not forced to characters prior to transfer to xtable? 2) Aside from using signif() or round() on each numeric element within my matrix as I create the variable "table", is there a way to get my numeric elements to have 4 significant figures prior to be manipulated by xtable()? Environment: R Studio 0.96.331 R version: 64-bit 2.15.2 Thank you for the help, Brian Hobbs Pulmonary and Critical Care Fellow Harvard Combined Program
David Winsemius
2012-Nov-08 17:40 UTC
[R] Formatting digits in a table with mix of numbers and characters
On Nov 8, 2012, at 8:14 AM, Brian Hobbs wrote:> Hello, > > I am currently trying to find an easy way to take a table with a mix > of numbers and characters and format the numbers in the table to be > constrained to 4 significant digits. > > Example: > > #this is my table creation >> table <- matrix(c(12.34567, "--", 10, 12.34567, "--", "NA", "--", 123.45678, "--"), ncol = 3, byrow=TRUE) >> >> table > [,1] [,2] [,3] > [1,] "12.34567" "--" "10" > [2,] "12.34567" "--" "NA" > [3,] "--" "123.45678" "--" > > #I would like for it to be > [,1] [,2] [,3] > [1,] "12.35" "--" "10" > [2,] "12.35" "--" "NA" > [3,] "--" "123.5" "--"Appears from your desired output that your interpretation of "significant digits differs from that of the format function's implementation. (See below)> > I am using the "xtable" package and attempt to use the digits() > argument within xtable; however, this did not format the numbers, > likely because my table was forced to character values when I created > it using matrix(). I am not bothered by the character values in my > final xtable result; however... > > I have two questions: > 1) Is there a better way to create my table so that the original > numeric values are not forced to characters prior to transfer to > xtable?I cannot think of any at the moment.> 2) Aside from using signif() or round() on each numeric element within > my matrix as I create the variable "table", is there a way to get my > numeric elements to have 4 significant figures prior to be manipulated > by xtable()?Not sure.> table <- matrix(c(12.34567, "--", 10, 12.34567, "--", "NA", "--", 123.45678, "--"), ncol = 3, byrow=TRUE) > table[] <- ifelse(!is.na(as.numeric(table)), format(as.numeric(table),digits=4), table)Warning messages: 1: In ifelse(!is.na(as.numeric(table)), format(as.numeric(table), digits = 4), : NAs introduced by coercion 2: In format(as.numeric(table), digits = 4) : NAs introduced by coercion> table[,1] [,2] [,3] [1,] " 12.35" "--" " 10.00" [2,] " 12.35" "--" "NA" [3,] "--" "123.46" "--" -- David.> > > > Environment: R Studio 0.96.331 > R version: 64-bit 2.15.2 > > > Thank you for the help, > Brian Hobbs > Pulmonary and Critical Care Fellow > Harvard Combined Program > > ______________________________________________ > R-help at r-project.org mailing list > 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.David Winsemius, MD Alameda, CA, USA
Marc Schwartz
2012-Nov-08 17:54 UTC
[R] Formatting digits in a table with mix of numbers and characters
On Nov 8, 2012, at 10:14 AM, Brian Hobbs <brhobbs at gmail.com> wrote:> Hello, > > I am currently trying to find an easy way to take a table with a mix > of numbers and characters and format the numbers in the table to be > constrained to 4 significant digits. > > Example: > > #this is my table creation >> table <- matrix(c(12.34567, "--", 10, 12.34567, "--", "NA", "--", 123.45678, "--"), ncol = 3, byrow=TRUE) >> >> table > [,1] [,2] [,3] > [1,] "12.34567" "--" "10" > [2,] "12.34567" "--" "NA" > [3,] "--" "123.45678" "--" > > #I would like for it to be > [,1] [,2] [,3] > [1,] "12.35" "--" "10" > [2,] "12.35" "--" "NA" > [3,] "--" "123.5" "--" > > I am using the "xtable" package and attempt to use the digits() > argument within xtable; however, this did not format the numbers, > likely because my table was forced to character values when I created > it using matrix(). I am not bothered by the character values in my > final xtable result; however... > > I have two questions: > 1) Is there a better way to create my table so that the original > numeric values are not forced to characters prior to transfer to > xtable? > 2) Aside from using signif() or round() on each numeric element within > my matrix as I create the variable "table", is there a way to get my > numeric elements to have 4 significant figures prior to be manipulated > by xtable()?Some things to note: 1. First, note that 'table' is coerced to a character matrix, since a matrix can only hold one data type. So, even though you included numeric values in the creation, they were coerced to character in the resultant object. 2. I would not be concerned with whether xtable gets a numeric or character based object. You are outputting to LaTeX, which is all going to be character based anyway. I would focus on getting the data into a format that you want and then simply use xtable() to generate the LaTeX markup. 3. Although R is typically smart enough to differentiate between an object named 'table' and the built-in function table(), I would avoid it, because there may be times when you will run into a problem and be scratching your head as to the source of the error. Vec <- c(12.34567, "--", 10, 12.34567, "--", "NA", "--", 123.45678, "--") # Warnings about NA's will occur here Vec.new <- ifelse(!is.na(as.numeric(Vec)), signif(as.numeric(Vec), 4), Vec)> Vec.new[1] "12.35" "--" "10" "12.35" "--" "NA" "--" "123.5" [9] "--"> matrix(Vec.new, ncol = 3, byrow = TRUE)[,1] [,2] [,3] [1,] "12.35" "--" "10" [2,] "12.35" "--" "NA" [3,] "--" "123.5" "--" Regards, Marc Schwartz