Why is Kable adding a non-existent column to my table?

Viewed 887

I have imported data from a CSV and created a table from this data. The code below is a (admittedly probably round about way) of getting the data together in a way I would like it presented:

sc_data_specialty_1_count<-as.vector(table(sc_data$SPECIALTY_1)) #CREATE TABLE OF SPECIALTY_1 COUNTS
sc_data_specialty_1_percent<-as.vector(table(sc_data$SPECIALTY_1)/nrow(sc_data)) #CREATE TABLE OF SPECIALTY_1 PERCENT.
sc_data_specialty_1_table<-rbind(sc_data_specialty_1_count,sc_data_specialty_1_percent*100) #COMBINE TABLES
sc_data_specialty_1_table<-round(sc_data_specialty_1_table,digits = 0) #ROUND TABLE
rownames(sc_data_specialty_1_table)<-c("Count","Proportion (%)") #ADD ROW NAMES

This matrix (sc_data_specialty_1_table) looks as follows:

             [,1] [,2] [,3] [,4] [,5] [,6] [,7]
Count          2   30    1    2    8    1    1
Proportion (%) 4   67    2    4   18    2    2

Note there are seven columns. I try to produce a kable table in RMarkdown with the following code:

kable(sc_data_specialty_1_table, col.names=c("ENT","General Surgery","Neurosurgery","Ophthalmology","Orthopedic","PM&R","Urology"),format="latex",booktabs=TRUE, caption = "Professors by specialty") %>%  #Creates an RMarkdown table using kable()

kable_styling(latex_options=c("scale_down","hold_position","striped")) #Scales down the table to fit in the page width.

I get an error trying to tell me that the names supplied for the columns does not fit the number of columns, but I have given seven names:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent
Calls: <Anonymous> ... eval -> eval -> %>% -> eval -> eval -> kable -> 
colnames<-
Execution halted

When i strip down the Kable to its most basic command, kable(sc_data_specialty_1_table), the output is as follows:

kable output

For whatever reason, Kable is adding the first column of data that doesn't (and shouldn't) actually exist in sc_data_specialty_1_table.

When I was working on this particular markdown document yesterday, I was not having this issue. I hadn't changed any code between yesterday and today. This is happening on the multiple tables in my markdown that I have constructed similarly.

  1. Does anyone know why this extra column is being added?
  2. If you can interpret what kind of table I am trying to make (raw count in the top row, proportion (%) of that specialty in the bottom row) and knows of a simpler way to put that together in a table than what I'm doing, that would be helpful.

Thank you.

1 Answers

Works fine with sampled data.frame.

library(knitr)

df <- data.frame(Count = c(2, 30, 1, 2, 8, 1, 1),
                 Proportion = c(4, 67, 2, 4, 18, 2, 2))


kable(
  t(df),
  col.names = c(
    "ENT",
    "General Surgery",
    "Neurosurgery",
    "Ophthalmology",
    "Orthopedic",
    "PM&R",
    "Urology"
  ),
  caption = "Professors by specialty",
  row.names = T
)

|           | ENT| General Surgery| Neurosurgery| Ophthalmology| Orthopedic| PM&R| Urology|
|:----------|---:|---------------:|------------:|-------------:|----------:|----:|-------:|
|Count      |   2|              30|            1|             2|          8|    1|       1|
|Proportion |   4|              67|            2|             4|         18|    2|       2|
Related