Open In App

How to Change the Displayed Column Names in Flextable Output in R

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Flextable is a powerful R package designed for creating and customizing tables in a variety of formats, including Word and PowerPoint documents. It provides a range of functions to manipulate table appearance and structure, making it a great tool for reporting and presentations. One common task is to change the displayed column names to make them more descriptive or reader-friendly. This article will guide you through the process of renaming columns in a Flextable output in R Programming Language.

Installation and Setup

Before we start, ensure you have the flextable package installed. You can install it from CRAN using the following command:

install.packages("flextable")

Load the flextable package along with other necessary packages:

library(flextable)
library(dplyr)

Let's use the built-in mtcars dataset for this demonstration. We'll first create a Flextable from a subset of the dataset and then modify the displayed column names.

R
# Load the mtcars dataset
data(mtcars)

# Select a subset of the data
df <- mtcars[1:5, 1:5]

# Display the first few rows
head(df)

Output:

                   mpg cyl disp  hp drat
Mazda RX4 21.0 6 160 110 3.90
Mazda RX4 Wag 21.0 6 160 110 3.90
Datsun 710 22.8 4 108 93 3.85
Hornet 4 Drive 21.4 6 258 110 3.08
Hornet Sportabout 18.7 8 360 175 3.15

Step 1: Creating a Flextable

Create a Flextable from the selected data frame:

R
# Create a Flextable
ft <- flextable(df)
ft

Output:

Screenshot-2024-07-31-173453
Change the Displayed Column Names in Flextable Output in R

Step 2: Changing Column Names

To change the displayed column names in a Flextable, you can use the set_header_labels function. This function allows you to specify new names for the columns in your table.

R
# Change the displayed column names
ft <- set_header_labels(ft,
                        mpg = "Miles Per Gallon",
                        cyl = "Cylinders",
                        disp = "Displacement",
                        hp = "Horsepower",
                        drat = "Rear Axle Ratio")

Step 3: Customizing the Table

You can further customize the appearance of your Flextable. For example, you might want to adjust the font size, alignment, or add borders.

R
# Customize the Flextable
ft <- ft %>%
  fontsize(size = 10, part = "all") %>%
  align(align = "center", part = "all") %>%
  border_outer(border = officer::fp_border(color = "black", width = 1)) %>%
  border_inner_h(border = officer::fp_border(color = "gray", width = 0.5))
# Display the Flextable
ft

Output:

Screenshot-2024-07-31-173654
Change the Displayed Column Names in Flextable Output in R

Conclusion

Flextable provides a flexible and powerful way to create and customize tables in R. Changing the displayed column names is straightforward with the set_header_labels function, and you can further enhance your tables with additional formatting options. This makes Flextable an excellent choice for creating professional-quality tables for reports and presentations.


Next Article
Article Tags :

Similar Reads