Priit Mets
Priit Mets

Reputation: 495

How avoid row values inside kable table?

I have the following data frame

my_df<-structure(list(date = c("6/28/2021", "6/29/2021", "6/30/2021", 
"7/1/2021", "7/2/2021", "7/3/2021", "7/4/2021", "AVG", "$\\Delta$", 
"% $\\Delta$"), Price1= c("5", "5.4", "1.7", "2", 
"7.5", "", "", "6.6", "2.5", "4.7"), Price2= c(9.5, 5.8, 5.7, 
9.9, 4.2, 4.1, 4.8, 2, 9.6, 2.6), Price3= c(1.8, 1.3, 
0.1, 8.5, 0.7, 1.1, 4, 1.1, 5.2, 11.3)), row.names = c("8", 
"9", "10", "11", "12", "13", "14", "1", "15", "16"), class = "data.frame")

Why do I have row values to the left of Date column in my final table? This problem started when I update all packages in RStudio.

library(dplyr)
library(kable)
library(knitr)
####table with knitr
col_el<-function(df,x){
  column_spec(df,x, color = "black",
              background = ifelse(my_df[x] > 0 & my_df$date == c("$\\Delta$","% $\\Delta$"), "#C6EFCE", 
                                  ifelse(my_df[x] < 0 & my_df$date == c("$\\Delta$","% $\\Delta$"), "#FFC7CE","white")))
  
}

my_df[1:4] %>% 
  kable(col.names = c("Date","BMW","AUDI","MB"),
        caption = "Weekly prices", booktabs=TRUE,
        align = "lccc")  %>%  alignment
  kable_classic( html_font = "Calibri")  %>%  #,full_width = F
  column_spec(c(3), border_right = "2px solid black")   %>%
  col_el(.,2)  %>%
  col_el(.,3)  %>%
  col_el(.,4) 

Upvotes: 3

Views: 3271

Answers (1)

tacoman
tacoman

Reputation: 947

The column next to your dates are the row numbers of your data frame. You can omit these by specifying row.names = FALSE in the kable() function.

my_df[1:4] %>% 
  kable(col.names = c("Date","BMW","AUDI","MB"),
        row.names = FALSE,
        caption = "Weekly prices", booktabs=TRUE,
        align = "lccc")  %>%  alignment
  kable_classic( html_font = "Calibri")  %>%  #,full_width = F
  column_spec(c(3), border_right = "2px solid black")   %>%
  col_el(.,2)  %>%
  col_el(.,3)  %>%
  col_el(.,4) 

Upvotes: 5

Related Questions