rlatexcharactertabularmultirow

Multi-row latex table with R


I have this data frame :

mydf <- data.frame(rowFactor1 = c(rep("S4",6),rep("S5",6),rep("S6",6)),
                   rowFactor2 = rep(c("All","T1/T2(V)"), 9),
                   colFactor1 = rep(c(rep("Ml",2),rep("Mp",2),rep("Mra",2)), 3), 
                   x = c(
                     "8.73",
                     "0.46 / 8.28 ( 2 )",
                     "7.02",
                     "0.84 / 8.28 ( 3 )",
                     "0.55",
                     "0.90 / 1.20 ( 0 )",
                     "8.14",
                     "2.02 / 1.57 ( 5 )",
                     "0.55",
                     "2.90 / 1.28 ( 4 )",
                     "1.73",
                     "3.46 / 8.28 ( 6 )",
                     "6.02",
                     "0.87 / 8.90 ( 5 )",
                     "1.55",
                     "0.90 / 1.20 ( 1 )",
                     "6.24",
                     "2.02 / 1.57 ( 7 )"
                   ))

How to create a Latex table with multiple rows using mydf on R?

I tried to use the tabular function of the R package tables

tab1 <- tabular(Heading()*RowFactor(rowFactor1, spacing = 1, 
                                    c("S4","S5","S6"))*
                  Heading()*RowFactor(rowFactor2, 
                                      levelnames = c("All","T1/T2(V)")) ~ 
                  Heading()*Factor(colFactor1, 
                                   levelnames = c("Ml","Mp","Mra")),
                data = mydf)
latex(tab1)

I had this table without x's values (8.73, 0.46 / 8.28 ( 2 ) ...) ​​and filled with a single value 1.

enter image description here

Thanks for your help !


Solution

  • If you wish to use strings in the table, maybe convert your x factor to character:

    mydf$x <- as.character(mydf$x)
    

    Then include the x strings as follows:

    library(tables)
    
    tab1 <- tabular(Heading()*RowFactor(rowFactor1, spacing = 1, 
                                        c("S4","S5","S6"))*
                      Heading()*RowFactor(rowFactor2, 
                                          levelnames = c("All","T1/T2(V)")) ~ 
                      Heading()*Factor(colFactor1, 
                                       levelnames = c("Ml","Mp","Mra"))*
                      Heading()*(x)*Heading()*identity,
                      data = mydf)
    latex(tab1)
    

    Also consider alternatives to tabular for Latex tables:

    Tools for making latex tables in R

    Latex table using tabular