Data Science with R (AECN 896-05)
  • Syllabus
  • Lecture Notes
  • Assignments
  • Exercises

On this page

  • 1 Exercise 1
  • 2 Exercise 2
  • 3 Exercise 3: Merging and Grouping Columns
  • 4 Exercise 4: Custom Header and Footer

Ex-8-1: Make tables with flextable

1 Exercise 1

Dataset: mtcars

  1. Load the mtcars dataset into R.
  2. Use the flextable package to create a table that displays the first 10 rows of the dataset for mpg, cyl, disp, and hp.
  3. Apply styles to the table to make
    1. the texts in the header bold using bold()
    2. the texts of the cyl column center-aligned including the cyl in the header using align()
    3. the texts of the hp column colored red using color()
    4. the font size of the texts of the mpg 18 if the value of disp is greater than 200 using fontsize().

Here is the finished table you are trying to make:

mpg

cyl

disp

hp

21.0

6

160.0

110

21.0

6

160.0

110

22.8

4

108.0

93

21.4

6

258.0

110

18.7

8

360.0

175

18.1

6

225.0

105

14.3

8

360.0

245

24.4

4

146.7

62

22.8

4

140.8

95

19.2

6

167.6

123

  • Work here
  • Answer
Code
# Create a simple flextable
ft <- 
  flextable(
    mtcars[1:10, ],
    col_keys = c("mpg", "cyl", "disp", "hp")
  ) %>%
  bold(part = "header") %>%
  align(j = 2, align = "center", part = "all") %>%
  color(j = 4, color = "red")

2 Exercise 2

Dataset: iris

  1. Load the iris dataset.
  2. Create a flextable that displays the first 10 rows of the dataset.
  3. Apply conditional formatting:
    1. Change the background color of the cells in the Sepal.Length column where their values are greater than 5.0 in blue using color using bg().
    2. Draw orange borders at the bottom of the cells in the Sepal.Width column if their values are less than 3.0 using border().
    3. Draw red borders at both sides of the cells in the Sepal.Width column if their corresponding values of Petal.Width are less than 1.5 using border().

Here is the finished table you are trying to make:

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

4.7

3.2

1.3

0.2

setosa

4.6

3.1

1.5

0.2

setosa

5.0

3.6

1.4

0.2

setosa

5.4

3.9

1.7

0.4

setosa

4.6

3.4

1.4

0.3

setosa

5.0

3.4

1.5

0.2

setosa

4.4

2.9

1.4

0.2

setosa

4.9

3.1

1.5

0.1

setosa

  • Work here
  • Answer
Code
# Create flextable for first 10 rows
ft <- 
  flextable(iris[1:10,]) %>%
  # Apply conditional formatting to Sepal.Length column
  bg(i = ~ Sepal.Length > 5, j = "Sepal.Length", bg = "blue") %>%
  border(i = ~ Sepal.Width > 3, j = ~ Sepal.Width, border.bottom = officer::fp_border(color = "orange"))

3 Exercise 3: Merging and Grouping Columns

Dataset: airquality

  1. Load the airquality dataset and filter the data so that you have only the first 10 days of May and June.
  2. Create a flextable using selected columns: Month, Day, Ozone, Wind.
  3. Group the table by the Month column and merge the cells of the Month column using merge_v().
  4. Draw a black border to separate May and June observations
  5. Draw a black line at the bottom of the table which was somehow lost in step 3 using fix_border_issues().
  6. Add a caption to the table using set_caption().

Here is the finished table you are trying to make:

Month

Day

Ozone

Wind

5

1

41

7.4

2

36

8.0

3

12

12.6

4

18

11.5

5

14.3

6

28

14.9

7

23

8.6

8

19

13.8

9

8

20.1

10

8.6

6

1

8.6

2

9.7

3

16.1

4

9.2

5

8.6

6

14.3

7

29

9.7

8

6.9

9

71

13.8

10

39

11.5

  • Work here
  • Answer
Code
# Select columns
ft <- 
  airquality %>%
  dplyr::filter(Month %in% c(5, 6) & Day <= 10) %>%
  .[, c("Month", "Day", "Ozone", "Wind")] %>%
  # Create flextable
  flextable() %>%
  # Merge Month column
  merge_v(j = "Month") %>%
  border(i = 10, border.bottom = officer::fp_border(color = "black")) %>%
  fix_border_issues() %>%
  # Add a caption
  set_caption("Airquality Data Table with Grouped Month")

4 Exercise 4: Custom Header and Footer

Dataset: iris

  1. Load the iris dataset.
  2. Create a table with the first 10 rows of the dataset.
  3. Modify the header by renaming columns Sepal.Length and Sepal.Width to Sepal Length and Sepal Width, respectively.
  4. Add a custom footer explaining what the dataset represents.

Here is the finished table you are trying to make:

Sepal Length

Sepal Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

4.7

3.2

1.3

0.2

setosa

4.6

3.1

1.5

0.2

setosa

5.0

3.6

1.4

0.2

setosa

5.4

3.9

1.7

0.4

setosa

4.6

3.4

1.4

0.3

setosa

5.0

3.4

1.5

0.2

setosa

4.4

2.9

1.4

0.2

setosa

4.9

3.1

1.5

0.1

setosa

Iris dataset: Measurements of iris flowers from three species

  • Work here
  • Answer
Code
ft <- 
  flextable(iris[1:15,]) %>%
  # Modify header labels
  set_header_labels(Sepal.Length = "Sepal Length", Sepal.Width = "Sepal Width") %>%
  # Add a footer
  add_footer_lines("Iris dataset: Measurements of iris flowers from three species")
 

Made with Quarto