adding headers and rows in table

I am trying to add a header above the table.

the objective is to add header above column name "#" and also I want to convert the table in flex table to apply flextable themes.

also i want to group the table on the basis of car type . any solution for that

df <- data.frame(names= c("Mazda RX4",  "Mazda RX4 Wag",    "Datsun 710",   "Hornet 4 Drive",   "Hornet Sportabout",    "Valiant",  "Duster 360",   "Merc 240D",    "Merc 230", "Merc 280", "Merc 280C",    "Merc 450SE",   "Merc 450SL",   "Merc 450SLC",  "Cadillac Fleetwood"),
                          score = c(21, 21, 22.8,   21.4,   18.7,   18.1,   14.3,   24.4,   22.8,   19.2,   17.8,   16.4,   17.3,   15.2,   10.4) )


table1 <- df %>% rownames_to_column("#") %>% rename("Car type" = names) %>% 
  mutate(score = as.character(score)) %>% 
  add_row(`Car type` = "", score = "form", .before = "#") 

Your question is not clear and confusing (what is Flextable in relation to your question).
In the spirit of this question the following answer:

library(tibble)
df <- mtcars %>%
  rownames_to_column(var="carnames") 
df <- df[,c(1:2)] %>% as.data.frame()
names(df[1]) <- "Names" # does not work
names(df)
#> [1] "carnames" "mpg"
names(df)[1] <- "Names" # does work (but why it does?)
names(df)
#> [1] "Names" "mpg"
Created on 2021-07-19 by the reprex package (v2.0.0)

I just updated the question

I did not succeed in adding the header row and also I had trouble with printing the row headers ('top class' etc). This is my best result:

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(flextable) # https://ardata-fr.github.io/flextable-book/index.html
library(officer)

df <- data.frame(names= c("Mazda RX4",  "Mazda RX4 Wag",    "Datsun 710",   "Hornet 4 Drive",   "Hornet Sportabout",    "Valiant",  "Duster 360",   "Merc 240D",    "Merc 230", "Merc 280", "Merc 280C",    "Merc 450SE",   "Merc 450SL",   "Merc 450SLC",  "Cadillac Fleetwood"),
                 score = c(21, 21, 22.8,   21.4,   18.7,   18.1,   14.3,   24.4,   22.8,   19.2,   17.8,   16.4,   17.3,   15.2,   10.4),
                 cclass= c(rep("class top",3),rep("class medium",3),rep("class lower",3),rep("unknown",6) )     )

table1 <- df %>% rownames_to_column("rank") %>% rename(cartype = names) %>% 
  mutate(score = format(score,digits=3)) %>% 
  select(cclass,rank,cartype,score) 

ft1 <- flextable::flextable(table1) %>%
  border( border = fp_border(width = 2, color = "#007FA6"), part = "all" ) %>%
  rotate(j=1,rotation="btlr") %>% 
  merge_v(j = 1) %>%
  set_header_labels(cclass = "", cartype="Car type", rank="#")  %>%
  autofit(part = "all") 

ft1
Created on 2021-07-20 by the reprex package (v2.0.0)

This produces the following table:

library(tidyverse)

df <- data.frame(names= c("Mazda RX4",  "Mazda RX4 Wag",    "Datsun 710",   "Hornet 4 Drive",   "Hornet Sportabout",    "Valiant",  "Duster 360",   "Merc 240D",    "Merc 230", "Merc 280", "Merc 280C",    "Merc 450SE",   "Merc 450SL",   "Merc 450SLC",  "Cadillac Fleetwood"),
                 score = c(21, 21, 22.8,   21.4,   18.7,   18.1,   14.3,   24.4,   22.8,   19.2,   17.8,   16.4,   17.3,   15.2,   10.4) )

library(flextable)
library(officer)
big_border = fp_border(color="black", width = 2)
small_border = fp_border(color="gray", width = 1)

table1 <- df %>% rownames_to_column("#") %>% rename("Car type" = names) 
t2 <- flextable(table1) %>% add_header_row(values=c(" "," ","form")) 
t2 <- border_remove(x = t2)
t3 <- border_inner(t2, part="all", border = small_border )
t4 <- border_outer(t3,part="header", border= big_border)
t5 <- align(t4, i=1, align="right", part="header")

t5
1 Like

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.