Data import and organization:

I load the gapminder and kableExtra libraries, and before I create the table, I begin filtering and arranging the data based on the year and continents of interest. Since the table contains the least populous countries by continent, I use slice_tail to select the least populated countries for the continents Americas and Europe in 1997.

To replicate the table, I also need to make sure that I order the data frame in the same way. It appears that the countries are listed by those with the highest life expectancy first, so I arrange my data frame accordingly.

Some variable names in the data set are abbreviated (e.g., pop for population), so I rename those using the group_by function. I also use pivot_longer and pivot_wider to move the population and life expectancy columns to rows and country names to columns.

A few adjustments using kable and kableExtra:

The data is ready to be plugged into kable and use some kableExtra features to add some specific details. I first make sure to set the decimal places to zero in kable to round my data appropriately. Using kableExtra functionality, I add stripes, change the position (left-aligned), and add headers to include the continents. Finally, I italicize the text in the statistic column and add a footnote with styling that matches the original table.

Replication of original table:

library(tidyverse)
library(gapminder)
library(kableExtra)
options(knitr.table.format = "html") 

least_pop_countries_1997 <- gapminder %>% drop_na() %>% filter(year == 1997 & continent %in% c("Americas", "Europe")) %>% group_by(continent) %>% select(continent, country, pop, lifeExp) %>% arrange(desc(pop)) %>% slice_tail(n = 2) 

least_pop_countries2 <- least_pop_countries_1997 %>% group_by(continent, country, pop) %>% select(continent, country, pop, lifeExp) %>% arrange(lifeExp, .by_group = TRUE)

least_pop_stat <- least_pop_countries2 %>% group_by(country, "population" = pop, "life expectancy" = lifeExp) %>% select() %>% pivot_longer(cols = c('population', 'life expectancy'), names_to = "statistic") %>% pivot_wider(names_from = country) %>% ungroup()


least_pop_stat  %>%
  kable(digits = 0) %>%
  kable_styling("striped", full_width = F, position = "left") %>% add_header_above(c(" " = 1, "Americas" = 2, "Europe" = 2)) %>% column_spec(1, italic = T) %>% footnote(general = "Population and life expectancy for the two least populous countries in Americas and Europe in 1997.", footnote_as_chunk = T, title_format = c("italic", "underline"))
Americas
Europe
statistic Jamaica Trinidad and Tobago Iceland Montenegro
population 2531311 1138101 271192 692651
life expectancy 72 69 79 75
Note: Population and life expectancy for the two least populous countries in Americas and Europe in 1997.