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:
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.
- Does anyone know why this extra column is being added?
- 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.
