4

I'm working with the tableContinuous function of the reporttools package in R.

Everything works as expected and using some example from docs:

library("reporttools")
data(CO2)
vars <- CO2[, 4:5]
group <- CO2[, "Treatment"]
weights <- c(rep(1, 60), rep(0, 10), rep(2, 14))

tableContinuous(vars = vars, weights = weights, subset = 
                  c(rep(TRUE, 57), rep(FALSE, 100 - 57)), group = group, prec = 3, print.pval = 
                  "kruskal", cap = "Table of continuous variables.", lab = "tab: descr stat")

I get a table as expected:

enter image description here

Is it possible to remove the all lines from the output?

www
  • 38,575
  • 12
  • 48
  • 84
radek
  • 7,240
  • 8
  • 58
  • 83

1 Answers1

2

No, such an option is not available in tableContinuous. The all lines are omitted only in case if there is only one level per variable, i.e. see if (n.levels == 1) ... at the end of the source of tableContinuous.

However, this problem can be solved using regular expressions. I am not an expert in it so there might be better ways.

library(reporttools)
data(CO2)
vars <- CO2[, 4:5]
group <- CO2[, "Treatment"]
weights <- c(rep(1, 60), rep(0, 10), rep(2, 14))

result <- tableContinuous(vars = vars, weights = weights, subset = 
                            c(rep(TRUE, 57), rep(FALSE, 100 - 57)), group = group, prec = 3, print.pval = 
                            "kruskal", cap = "Table of continuous variables.", lab = "tab: descr stat")

Completely removing all lines:

cat(gsub("\\\\hline\n[^\n]+& all &[^\n]+\n", "", result))

Keeping p-values:

greg <- gregexpr("p (=|<) [^\n]+", result)
regmatches(result, greg) <- list(gsub("(?<=&)[-.\\w ]+", " ", 
                                     regmatches(result, greg)[[1]], perl = TRUE))
cat(result)
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • 1
    @radek, is there anything I could add to my answer? – Julius Vainora Jun 03 '13 at 20:49
  • I'm sorry for late feedback. Finally had time to work with your code. First solution works without a flaw. Using the second solution I also get the correct output, however, when generating kniting pdf file less than symbol for p value is not displayed correctly. Could you please point me to relevant part of code I need to change (I guess `<` should be replaced with LATEX's `\textless`?) – radek Jun 05 '13 at 13:25
  • 1
    @radek, is it only the symbol `<` that is wrong? I guess that the lines with `p <` were not removed at all. I updated my answer now by changing `gregexpr("p =` to `gregexpr("p (=|<)` to fix that. It now also includes a case with negative numbers (changed `[.\\w ]+` to `[-.\\w ]+`). In case you still want to change `p <` to `p \textless` try `gsub("p <", "p \\\\textless", result)`. I hope that helps. – Julius Vainora Jun 05 '13 at 14:23
  • Updated solution still doesn't want to print `<` correctly. Also - when you change the option of `reporttolls` from ` print.pval = "kruskal"` to ` print.pval = "anova"` it messes up with LATEX code and doesn't display correctly formatted table in pdf document. – radek Jun 05 '13 at 15:20
  • 1
    Is `<` printed correctly before applying modifications from my answer? I can't help with `print.pval = "anova"` either, because using this option in your example does not ruin anything for me. So you will have to give me some more details. – Julius Vainora Jun 05 '13 at 15:42
  • No it wasn’t printed correctly either. `anova` issues are socondary to me at the moment so please ignore this issue. – radek Jun 05 '13 at 15:55
  • 1
    If it wasn't printed correctly, then I can't really help here since it seems to be more related to knitr. As I mentioned in the previous comment, in case you find what to replace `<` with, you could additionally use `gsub` after all the other modifications. – Julius Vainora Jun 05 '13 at 16:04
  • All right then - I understand. Priority for me is table without p now and that you sorted perfectly. So thanks again for your effort! =) – radek Jun 05 '13 at 17:04