0

I am using kableExtra to format tables in rmarkdown. In one of my dataframes I am obtaining a strange result...

ncol(Selected)
Selected%>%
kable("html",format.args = list(decimal.mark = ".", big.mark = ","),caption = "Variables description  and data cleaning and treatment summary") %>%
add_header_above(c(" " = 2, "Kolmogorov-Smirnov" = 2, "Kruskall-Wallis" = 2, "Wilcoxon  Test"= 2)) %>%
kable_styling()

The object Selected is a dataframe, with dimension (256;8)...

I got this:

[1] 8
Error in htmlTable_add_header_above(kable_input, header, bold, italic, : The new header row you provided has a different total number of columns with the original kable output.

What is wrong? By ncol, I confirmed the object Selected has 8 columns... Thanks in advance.

hamagust
  • 728
  • 2
  • 10
  • 28
  • 1
    You can't expect any specific advice without providing [a minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – David Klotz May 08 '18 at 15:57
  • 1
    Hi, David, thank you for giving attention to my question. I agree that this question does not have a minimal reproducible example as I did not make available the data frame. Could you tell me how can I do this? I tried when I was writing the question, but I was not able to figure out... – hamagust May 08 '18 at 17:59
  • I think I figure out the reason for the error message... while trying to better describe the dataframe (as I was not able to append it), I noticed that this data frame has rownames, whereas others do not... I tried to change the add_header_above(c(" " = 2... to add_header_above(c(" " = 3... and it worked! – hamagust May 08 '18 at 18:08

1 Answers1

2

This data frame has row names. The kable command counts row names as the column. Counting this, it worked.

Selected%>%
kable("html",format.args = list(decimal.mark = ".", big.mark = ","),caption = "Variables description  and data cleaning and treatment summary") %>%
add_header_above(c(" " = 3, "Kolmogorov-Smirnov" = 2, "Kruskall-Wallis" = 2, "Wilcoxon  Test"= 2)) %>%
kable_styling()
hamagust
  • 728
  • 2
  • 10
  • 28