-1

I try to make table and want to make the first col aligned to left and the rest aligned to center. It seems like no align option for col_spec. how should I do?

Here are the sample codes that I made:

x<-data.frame("Name"=c("test", "try"),"SN"=1:2, "Age"=c(21, 15), "Salary"=c(1000,2000))

x %>% kable("html", align="c") %>% kable_styling(full_width = FALSE, position="left" )%>% row_spec(0, background = "#F3E2A9")

Stataq
  • 2,237
  • 6
  • 14

1 Answers1

1

You can specify the alignment based on the order of the columns:

x<-data.frame("Name"=c("test", "try"),"SN"=1:2, "Age"=c(21, 15), "Salary"=c(1000,2000))
library(kableExtra)
library(knitr)
x %>% 
  kable("html", align=c("l", 'c', 'c','c')) %>% 
  kable_styling(full_width = FALSE, position="left")%>% 
  row_spec(0, background = "#F3E2A9")

In your example, the fist column is aligned to the left, the remaining three columns are aligned to the center.

Susan Switzer
  • 1,531
  • 8
  • 34
  • Thanks for the prompt reply. Is it possible to get first col's title to be aligned center, but the contents aligned to left? in another words, keep row 0 to be centered? – Stataq Jun 01 '20 at 20:29
  • x<-data.frame("Name"=c("test", "try"),"SN"=1:2, "Age"=c(21, 15), "Salary"=c(1000,2000)) library(kableExtra) library(knitr) x %>% kable("html", align=c("l", 'c', 'c','c')) %>% kable_styling(full_width = T, position="left")%>% row_spec(0, background = "#F3E2A9", align = 'c') – Susan Switzer Jun 01 '20 at 21:42