How to apply a different number format to each line in R

I have a table that I want to display that contains a combination of percent, integer and floating point values ​​in different rows of the table.

I would like to create formatted output in an R markup or Rnw file that displays my table well.

I can do this based on the column if I use the xtable command and the numbers command.

I researched the use of the print.dll.to.row function in LaTeX output, however I cannot find the LaTeX tag that will apply the format to my numbers, all of them will require brackets around individual numbers for the formatting to take effect, and I don’t I can achieve this.

<<results=tex>>=
  library(xtable)

  ### some random data
  X<-data.frame();
  for (i in 1:2) {
    r<-sample(1:1000,4);
    X<-rbind(X,r);
  }
  X<-rbind(X,(X[1,]/(X[1,]+X[2,])))
  colnames(X)<-c("Cat A","Cat B","Cat C","Cat D")
  rownames(X)<-c("Passed","Failed","Percent Passed")

  ###Atempts to format the rows  
  formTable<-xtable(X)
  print(formTable)

  ###This works to format column
  Y=t(X)
  Y[,"Percent Passed"]=Y[,"Percent Passed"]*100;
  formatedTab<-xtable(Y)
  digits(formatedTab)<-c(0,0,0,2)
  print(formatedTab)
 @

If I could carry the output of the second table, I would be happy ... but this does not work.

, , , , .

+4
1

xtable

library(xtable)
set.seed(10)
X <- as.data.frame(matrix(sample(1:1000, 8), nrow = 2), stringsAsFactors = FALSE)
X[3, ] <- (X[1,]/(X[1,]+X[2,])) * 100
X
#         V1        V2        V3        V4
#1 508.00000 427.00000  85.00000 273.00000
#2 307.00000 692.00000 225.00000 271.00000
#3  62.33129  38.15907  27.41935  50.18382
X <- as.data.frame(lapply(X, sprintf, fmt = c("%.0f", "%.0f", "%.6f")))
colnames(X)<-c("Cat A","Cat B","Cat C","Cat D")
rownames(X)<-c("Passed","Failed","Percent Passed")
formTable<-xtable(X)
formTable
#% latex table generated in R 3.2.0 by xtable 1.7-4 package
#% Tue May  5 11:37:51 2015
#\begin{table}[ht]
#\centering
#\begin{tabular}{rllll}
#  \hline
# & Cat A & Cat B & Cat C & Cat D \\ 
#  \hline
#Passed & 508 & 427 & 85 & 273 \\ 
#  Failed & 307 & 692 & 225 & 271 \\ 
#  Percent Passed & 62.331288 & 38.159071 & 27.419355 & 50.183824 \\ 
#   \hline
#\end{tabular}
#\end{table}
+3

All Articles