Merging column names in rmarkdown tables

animal <- c("new","old","new","old")
price <- c(150,200,500,750)
animal_data <- rbind(animal,price)
colnames(animal_data) <- c("lion", "lion2", "buffalo", "buffalo2")
animal_data<- data.frame(animal_data)
animal_data

the data look like the ff.
image

How we can merge only column names lion and lion2 =''lion'' $ buffalo and buffalo2= ''buffalo'' in rmarkdown kable() function , could you have an idea?

It is not really clear what you are trying to do but I think your attempt to create a data.frame will not work. My best guess in that you are trying to do something like this.

lion <-"newt"  # new chanced to "newt" because "new" is a function name.
lion2 <- "old"
buffalo <- "newt"  # new chanced to "newt" because "new" is a function name.
buffalo2<- "old"
animal <- c(lion, lion2, buffalo, buffalo2)
price <- c(150,  200, 500, 750)
animal_data <- data.frame(animal,price)
animal_data

This gives us this, which I suspect is not what you want.

 animal price
1   newt   150
2    old   200
3   newt   500
4    old   750

thank you for your help. you are correct the code was not correct. I have edit the code . my question is how we can merge two column names in to one single name in rmarkdown. if you have an idea please help me . thank you in advance

Perhaps you could edit the question to show what result you want.

I have edited see again

animal <- c("new","old","new","old")
price <- c(150,200,500,750)
animal_data <- rbind(animal,price)
colnames(animal_data) <- c("lion", "lion2", "buffalo", "buffalo2")
animal_data<- data.frame(animal_data)
animal_data

Yes, but what should the modified table (the final result) look like?

image
i want only two column names lion and buffalo, assume loin & lion2 are the same

This is the output your code currently produces. Are you saying you want the same 2x4 table, but with column names "lion, lion, buffalo, buffalo"? Repeated column names are not a good idea.

So, how we can merge lion, lion to lion only ,two cells in one, this my question , the output should like the following

               lion                     buffalo                                                                            
   new         old             new           old
    150           200             500            750

I don't think you can do that with just kable, but you can if you install the kableExtra package. There is an example here.

animal <- c("new","old","new","old")
price <- c(150,200,500,750)
animal_data <- rbind(animal,price)
animal_data<- data.frame(animal_data)
library(knitr)
library(kableExtra)
kable(animal_data, booktabs = TRUE) %>%
  add_header_above(c(" " = 1, "lion" = 2, "buffalo" = 2))

still by default it gives x1 and x2 column names


col_name<-  c("new","old","new","old")
price <- c(150,200,500,750)
weghts<- c(220, 330,800,900)
animal_data <- rbind(price,weghts)
colnames(animal_data) <- col_name
animal_data<- data.frame(animal_data)

# by defual if the column names duplicate , it gives .1 after the duplicate name 
names(animal_data) <- gsub('[.].+', '', names(animal_data))
library(knitr)
library(kableExtra)
kable(animal_data) %>%
  add_header_above(c(" " = 1, "lion" = 2, "buffalo" = 2))%>%kable_styling(latex_options = "scale_down")

thank you for your link, now I got, how I can prepare r markdown tables in such cases
I modified the original code by adding one more rows to create a data.frame and use the new and old rows as column names, remove duplicates using gsub() function then add_headers_above function to add lion and buffalo main columns. thank you

This topic was automatically closed 7 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.