trying to modify column names of table

I am able to create a kable table with merged variable names but the columns names i am getting in output are wiered so now i want to change column names .

i want to change column names from T_0_BD to BD , T_0_HA to HA ......

year <- c("T_0", "T_1", "T_2", "T_3", "T_4", "T_5", "T_6", "T_7", "T_8", "T_9", "T_10")

BD <- c(28,26,24,22,20,19,17,15,13,11,9)

HA <- c(28,27,26,25,24,23,22,20,19,17,15)

PA <- c(28,27,25,24,23,22,21,21,21,22,22)

EA <- c(28,26,24,22,20,19,17,15,13,11,9)

AM <- c(28,28,27,27,27,27,27,27,28,30,31)

df <- data.frame(year,BD,HA,PA,EA,AM)

df1 <- df %>%
  pivot_longer(- year, names_to = "summary", values_to = "tabl")
df1 <- df1 %>%  filter(summary %in% c("BD", "HA"),year %in% c("T_0", "T_5", "T_10"))
df1 <- df1 %>%  pivot_wider(names_from = c("year", "summary"), values_from = "tabl")
df1 <- df1 %>% mutate(`Total criteria` = "basis") %>% relocate(`Total criteria`) 

df2 <- df1 %>% kbl(col.names[2:ncol(df1)] = c("BD1", "HA1","BD1", "HA1","BD1", "HA1")) %>% kable_classic() 

df2 <-  add_header_above(c( "today line" = 2, "last year" = 2, "next year" = 2))
df2 

the output should be like

image

rather than that change the columns of df1 like this

colnames(df1)[2:ncol(df1)] = c("BD1", "HA1","BD1", "HA1","BD1", "HA1")

df2 <- df1 %>% kbl() %>% kable_classic() 

df2 <- df2 %>% add_header_above(header=c( " "=1,"today line" = 2, "last year" = 2, "next year" = 2))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.