Search code examples
rlatexmultiple-columnsknitrtabular

How to add variables based on external data to a tabular{tables} summary table


Is it possible to add rows (or columns) to a summary table based on an external object with tabular() ?

This works:

tabular( (Sepal.Width+Petal.Width) ~ (n=1) + Format(digits=2)*(Species)*(mean + sd), data=iris )

This does not:

var <- names(iris)[c(2,4)]
tabular( (var) ~ (n=1) + Format(digits=2)*(Species)*(mean + sd), data=iris )

Is there a simple way to choose variables using another object, or do I really have to call them by their name?

Disclaimer: I'm new to both latex and the package {tables}, so please forgive my naivety.


Solution

  • Try this:

    tabular( parse(text = paste(var, collapse = "+")) ~ (n=1) + Format(digits=2)*(Species)*(mean + sd), data=iris )
    

    then you will get

                     Species                                    
                     setosa       versicolor      virginica     
                 n   mean    sd   mean       sd   mean      sd  
     Sepal.Width 150 3.43    0.38 2.77       0.31 2.97      0.32
     Petal.Width 150 0.25    0.11 1.33       0.20 2.03      0.27