Basic Tables

One can create an extremely basic table using just the kable() function and no formatting. This table feature no formatting and not incorporate any borders between cells.

You do not have to specify the format = argument, if you do not the table will be created regardless of your output type (word, html, etc.). Specifying this argument is mostly required for additional changes to the appearance of the table when using the kableExtra package. In the case below, we have specified a html output, if we attempted to knit this document to word or pdf the table would be created.

imdb_animation_avgs <- imdb %>%
  filter(type != "videoGame")%>%
  group_by(type, animation)%>%
  summarise(Mean.Votes = mean(numVotes, na.rm = TRUE),
            Mean.Rating = mean(averageRating, na.rm = TRUE),
            Number.Of.Entries = n())

knitr::kable(imdb_animation_avgs, "html") #basic table with no editing of appearance
type animation Mean.Votes Mean.Rating Number.Of.Entries
movie FALSE 17337.709 6.254202 41085
movie TRUE 36393.630 6.656642 1197
short FALSE 1576.763 6.823500 1183
short TRUE 1822.737 7.363987 933
tvMiniSeries FALSE 5779.093 7.452980 906
tvMiniSeries TRUE 2829.885 7.411458 96
tvMovie FALSE 1857.997 6.093077 3019
tvMovie TRUE 2162.584 7.100000 125
tvSeries FALSE 12953.661 7.208266 4682
tvSeries TRUE 7238.361 7.269250 1174
tvShort FALSE 1929.167 7.422917 48
tvShort TRUE 2902.484 7.179121 91
tvSpecial FALSE 1830.817 7.427378 431
tvSpecial TRUE 5492.600 7.820000 5
video FALSE 2556.080 5.559368 1425
video TRUE 5036.685 6.431738 397

include a title and change column names

Unlike other packages, you can set many formatting options within the call to kable() this includes arguments for the number of digits for numeric columns, row names, column names, column alignment and a caption. As well as many arguments which can be passed to the format.args = option.

Using the kableExtra package, we are then able to create a much nicer and cleaner looking output for our table. This package is mainly designed to improve the appearance for tables in html and pdf, with different options available for these. You can pipe in the kable_styling() function with no arguments which will provide a default sleeker looking appearance to the table. The appearance depends on the format specified in the kable function. The example below focuses on html.

top_20_movies <- imdb %>%
  filter(type == "movie" & numVotes > 99999)%>%
  arrange(desc(averageRating))%>%
  select(title, year, averageRating, numVotes)%>%
  slice(1:20)

knitr::kable(top_20_movies,
             "html",
             col.names = c("Movie", "Year", "Rating", "Total Votes"), #provide a string of column names
             caption = "IMDB Entries with at least 100,000 votes")%>% # provide a caption (title)
  kable_styling()
IMDB Entries with at least 100,000 votes
Movie Year Rating Total Votes
The Shawshank Redemption 1994 9.3 2138866
The Godfather 1972 9.2 1468315
The Godfather: Part II 1974 9.0 1021652
The Dark Knight 2008 9.0 2102907
The Mountain II 2016 9.0 101445
12 Angry Men 1957 8.9 610262
Schindler’s List 1993 8.9 1109973
Pulp Fiction 1994 8.9 1678715
The Lord of the Rings: The Return of the King 2003 8.9 1520719
The Good, the Bad and the Ugly 1966 8.8 635197
Forrest Gump 1994 8.8 1646150
The Lord of the Rings: The Fellowship of the Ring 2001 8.8 1536315
Fight Club 1999 8.8 1709679
Inception 2010 8.8 1875664
One Flew Over the Cuckoo’s Nest 1975 8.7 845858
Star Wars: Episode V - The Empire Strikes Back 1980 8.7 1070168
Goodfellas 1990 8.7 924279
The Matrix 1999 8.7 1540221
The Lord of the Rings: The Two Towers 2002 8.7 1375568
It’s a Wonderful Life 1946 8.6 363682

change number formatting and collapse rows

Additionally within the kable() function you can change number formatting by using the digits argument. You can do this by providing vector equal to the number of columns with the number of digits for each column if you wish to have columns with different arguments. Alternatively you can just provide a single number for a global option applicable to the whole table.

Similarly you can provide a string to the align argument to specify the column alignments, with “l” for left, “r” for right and “c” for centre. For instance if you write a string of “clc” then the first column will be centre aligned, the second left aligned and the third centre aligned.

You can use the collapse_rows() function to group same values in columns into multiple row cells. The main argument here is to provide the column you wish to apply this to. You can also specify the valign = argument by providing a string saying either top, middle, or bottom. This specifies the vertical alignment of the text in the grouped cell.

knitr::kable(imdb_animation_avgs,
             "html",
             col.names = gsub("[.]", " ", names(imdb_animation_avgs)), # specify column names, can use a formatting function instead of a string if you so wish
             caption = "Popularity and Quality of Animation",
             digits = c(0,0,0,2,0), # specify the digits for all columns
             format.args = list(big.mark = ","), # include a big mark for numbers above 1000
             align = "llrrr")%>% #RULER OF OMICRON PERSEI 8
            #provide a character string to set column aligmnets
  collapse_rows(columns = 1, valign = "middle")%>% # group 1st coulumn by the same value and align text in the middle of the cell
  kable_styling()
Popularity and Quality of Animation
type animation Mean Votes Mean Rating Number Of Entries
movie FALSE 17,338 6.25 41,085
movie TRUE 36,394 6.66 1,197
short FALSE 1,577 6.82 1,183
short TRUE 1,823 7.36 933
tvMiniSeries FALSE 5,779 7.45 906
tvMiniSeries TRUE 2,830 7.41 96
tvMovie FALSE 1,858 6.09 3,019
tvMovie TRUE 2,163 7.10 125
tvSeries FALSE 12,954 7.21 4,682
tvSeries TRUE 7,238 7.27 1,174
tvShort FALSE 1,929 7.42 48
tvShort TRUE 2,902 7.18 91
tvSpecial FALSE 1,831 7.43 431
tvSpecial TRUE 5,493 7.82 5
video FALSE 2,556 5.56 1,425
video TRUE 5,037 6.43 397

add a footnote

You can also add footnotes using the footnote() function. You can create different types of footnotes with different symbols using different arguments. general = will create an overall footnote with no symbol attached, number = will create a numeric symbol footnote, alphabet = will create an alphabetic footnote and symbol = will create footnotes using special symbols. Special symbols can also be manually specified.

These footnotes will not specify to a particular cell/title in the table unfortunately, something you can do with gt. In order to do this you need to edit the particular cell beforehand, or if it is a column name then you can do so within the col.names argument. You specify which footnote this applies to by using the footnote_marker_***() function. This specifies which list of footnotes to use and which symbol in the list to use. For example, using footnote_marker_alphabet(1) will put an “a” after the value, footnote_marker_alphabet(2) will put a “b” after the value and so on and so on.

If you are ever using these footnote marker functions, then within your call to kable, you have to include the escape = FALSE argument. This allows the function escape special characters when converting to the table. This will avoid any syntax errors created by these symbols.

top_20_movies[5,3] <- paste(top_20_movies[5,3], footnote_marker_symbol(1)) #sepcify the first sybmbol (* default)

knitr::kable(top_20_movies,
             "html",
             col.names = c(paste("Movie", footnote_marker_alphabet(1)), "Year", "Rating", "Total Votes"), #include and "a" agter the column name
             caption = "IMDB Entries with at least 100,000 votes",
             escape = F)%>% # ensure that html code is correctly honoured
  kable_styling()%>%
    footnote(general = "Here is a general comments of the table. ", #use vector of character strings to write multiple footnotes at once 
           number = c("Footnote 1; ", "Footnote 2; "),
           alphabet = c("Footnote A; ", "Footnote B; "),
           symbol = c("Footnote Symbol 1; ", "Footnote Symbol 2"))
IMDB Entries with at least 100,000 votes
Movie a Year Rating Total Votes
The Shawshank Redemption 1994 9.3 2138866
The Godfather 1972 9.2 1468315
The Godfather: Part II 1974 9 1021652
The Dark Knight 2008 9 2102907
The Mountain II 2016 9 * 101445
12 Angry Men 1957 8.9 610262
Schindler’s List 1993 8.9 1109973
Pulp Fiction 1994 8.9 1678715
The Lord of the Rings: The Return of the King 2003 8.9 1520719
The Good, the Bad and the Ugly 1966 8.8 635197
Forrest Gump 1994 8.8 1646150
The Lord of the Rings: The Fellowship of the Ring 2001 8.8 1536315
Fight Club 1999 8.8 1709679
Inception 2010 8.8 1875664
One Flew Over the Cuckoo’s Nest 1975 8.7 845858
Star Wars: Episode V - The Empire Strikes Back 1980 8.7 1070168
Goodfellas 1990 8.7 924279
The Matrix 1999 8.7 1540221
The Lord of the Rings: The Two Towers 2002 8.7 1375568
It’s a Wonderful Life 1946 8.6 363682
Note:
Here is a general comments of the table.
1 Footnote 1;
2 Footnote 2;
a Footnote A;
b Footnote B;
* Footnote Symbol 1;
Footnote Symbol 2

Special Features

text and cell colours

This is quite a complicated example but shows some of the extensions one can do within kable. This using cell_spec() to specify the format of a cell before sending the data frame to kable. The function will provide LaTex of html code formatting to the cells of your data frame including text colour, background colours, italic text, bold text etc. You can even use the spec_color() function which will apply a continuous colour gradient to your cells.

iris[1:10, ] %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "html", bold = T, color = spec_color(x, end = 0.9), # mutate the cells of the data frame directly, cell_spec will add in html or Latex coding which kable will be able to read
font_size = spec_font_size(x)) # specify font size, changes relative to range of values in the column in this case. A continous scale of font size and colour
}) %>%
mutate(Species = cell_spec(
Species, "html", color = "white", bold = T,
background = spec_color(1:10, end = 0.9, option = "C", direction = -1) #specify a background contninous colour gradient
)) %>%
kable("html", escape = F, booktabs = T, linesep = "", align = "c")%>% # remember the escape argument to make sure html code is read properly
  kable_styling()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 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 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

scroll box

There are many html only features that can be used within kableExtra that could especially be useful if ever wanting to create tables in a shiny application such as one of our data monitoring platforms.

This is done using the scroll_box() function in which you specify a character string defining the width and height of the box using either pixels or a percentage.

top_100_movies <- imdb %>%
  filter(type == "movie" & numVotes > 99999)%>%
  arrange(desc(averageRating))%>%
  slice(1:100)

kable(top_100_movies,
      "html")%>%
  kable_styling()%>%
  scroll_box(width = "500px", height = "400px") # specify size of the scroll box
title type year length numVotes averageRating director birthYear animation action adventure comedy documentary fantasy romance sci_fi thriller
The Shawshank Redemption movie 1994 142 2138866 9.3 Frank Darabont 1959 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Godfather movie 1972 175 1468315 9.2 Francis Ford Coppola 1939 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Godfather: Part II movie 1974 202 1021652 9.0 Francis Ford Coppola 1939 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Dark Knight movie 2008 152 2102907 9.0 Christopher Nolan 1970 FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Mountain II movie 2016 135 101445 9.0 Alper Caglar NA FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
12 Angry Men movie 1957 96 610262 8.9 Sidney Lumet 1924 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Schindler’s List movie 1993 195 1109973 8.9 Steven Spielberg 1946 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Pulp Fiction movie 1994 154 1678715 8.9 Quentin Tarantino 1963 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Lord of the Rings: The Return of the King movie 2003 201 1520719 8.9 Peter Jackson 1961 FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
The Good, the Bad and the Ugly movie 1966 161 635197 8.8 Sergio Leone 1929 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Forrest Gump movie 1994 142 1646150 8.8 Robert Zemeckis 1951 FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
The Lord of the Rings: The Fellowship of the Ring movie 2001 178 1536315 8.8 Peter Jackson 1961 FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
Fight Club movie 1999 139 1709679 8.8 David Fincher 1962 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Inception movie 2010 148 1875664 8.8 Christopher Nolan 1970 FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
One Flew Over the Cuckoo’s Nest movie 1975 133 845858 8.7 Milos Forman 1932 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Star Wars: Episode V - The Empire Strikes Back movie 1980 124 1070168 8.7 Irvin Kershner 1923 FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
Goodfellas movie 1990 146 924279 8.7 Martin Scorsese 1942 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Matrix movie 1999 136 1540221 8.7 Lana Wachowski 1965 FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
The Lord of the Rings: The Two Towers movie 2002 179 1375568 8.7 Peter Jackson 1961 FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
It’s a Wonderful Life movie 1946 130 363682 8.6 Frank Capra 1897 FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
Seven Samurai movie 1954 207 289616 8.6 Akira Kurosawa 1910 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Star Wars: Episode IV - A New Hope movie 1977 121 1139020 8.6 George Lucas 1944 FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
The Silence of the Lambs movie 1991 118 1158497 8.6 Jonathan Demme 1944 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Se7en movie 1995 127 1314485 8.6 David Fincher 1962 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Life Is Beautiful movie 1997 116 564954 8.6 Roberto Benigni 1952 FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
The Green Mile movie 1999 189 1039125 8.6 Frank Darabont 1959 FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
Saving Private Ryan movie 1998 169 1133391 8.6 Steven Spielberg 1946 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Spirited Away movie 2001 125 571049 8.6 Hayao Miyazaki 1941 TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
City of God movie 2002 130 654621 8.6 Kátia Lund 1966 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Interstellar movie 2014 169 1332041 8.6 Christopher Nolan 1970 FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
Avengers: Endgame movie 2019 181 565586 8.6 Anthony Russo 1970 FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
City Lights movie 1931 87 151102 8.5 Charles Chaplin 1889 FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
Modern Times movie 1936 87 195573 8.5 Charles Chaplin 1889 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
The Great Dictator movie 1940 125 184860 8.5 Charles Chaplin 1889 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Casablanca movie 1942 102 484406 8.5 Michael Curtiz 1886 FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
Rear Window movie 1954 112 407082 8.5 Alfred Hitchcock 1899 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Psycho movie 1960 109 549773 8.5 Alfred Hitchcock 1899 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Once Upon a Time in the West movie 1968 165 276867 8.5 Sergio Leone 1929 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Back to the Future movie 1985 116 956609 8.5 Robert Zemeckis 1951 FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
Grave of the Fireflies movie 1988 89 203114 8.5 Isao Takahata 1935 TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Cinema Paradiso movie 1988 155 206440 8.5 Giuseppe Tornatore 1956 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Terminator 2: Judgment Day movie 1991 137 924116 8.5 James Cameron 1954 FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
The Lion King movie 1994 88 862509 8.5 Roger Allers 1949 TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Léon: The Professional movie 1994 110 945821 8.5 Luc Besson 1959 FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Usual Suspects movie 1995 106 921947 8.5 Bryan Singer 1965 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
American History X movie 1998 119 966092 8.5 Tony Kaye 1952 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Gladiator movie 2000 155 1234507 8.5 Ridley Scott 1937 FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
The Pianist movie 2002 150 658270 8.5 Roman Polanski 1933 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Departed movie 2006 151 1094918 8.5 Martin Scorsese 1942 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
The Prestige movie 2006 130 1085577 8.5 Christopher Nolan 1970 FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
The Intouchables movie 2011 112 691362 8.5 Olivier Nakache 1973 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Whiplash movie 2014 106 633344 8.5 Damien Chazelle 1985 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Avengers: Infinity War movie 2018 149 710517 8.5 Anthony Russo 1970 FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
Sunset Blvd. movie 1950 110 183192 8.4 Billy Wilder 1906 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Paths of Glory movie 1957 88 160363 8.4 Stanley Kubrick 1928 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb movie 1964 95 421090 8.4 Stanley Kubrick 1928 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Alien movie 1979 117 727260 8.4 Ridley Scott 1937 FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
Apocalypse Now movie 1979 147 558918 8.4 Francis Ford Coppola 1939 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Shining movie 1980 146 800013 8.4 Stanley Kubrick 1928 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Raiders of the Lost Ark movie 1981 115 825793 8.4 Steven Spielberg 1946 FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Once Upon a Time in America movie 1984 229 283447 8.4 Sergio Leone 1929 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Aliens movie 1986 137 610070 8.4 James Cameron 1954 FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
Princess Mononoke movie 1997 134 301169 8.4 Hayao Miyazaki 1941 TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
Memento movie 2000 113 1045575 8.4 Christopher Nolan 1970 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Oldboy movie 2003 120 467919 8.4 Chan-wook Park 1963 FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
The Lives of Others movie 2006 137 328872 8.4 Florian Henckel von Donnersmarck 1973 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
WALL·E movie 2008 98 923571 8.4 Andrew Stanton 1965 TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Like Stars on Earth movie 2007 165 146744 8.4 Amole Gupte NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
3 Idiots movie 2009 170 307626 8.4 Rajkumar Hirani 1962 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
The Dark Knight Rises movie 2012 164 1408845 8.4 Christopher Nolan 1970 FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Django Unchained movie 2012 165 1238051 8.4 Quentin Tarantino 1963 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Coco movie 2017 105 296439 8.4 Lee Unkrich 1967 TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Spider-Man: Into the Spider-Verse movie 2018 117 261452 8.4 Bob Persichetti NA TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
Dangal movie 2016 161 129035 8.4 Nitesh Tiwari NA FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Your Name. movie 2016 106 144067 8.4 Makoto Shinkai 1973 TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
The Kid movie 1921 68 100324 8.3 Charles Chaplin 1889 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Metropolis movie 1927 153 147033 8.3 Fritz Lang 1890 FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
M movie 1931 117 130845 8.3 Fritz Lang 1890 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Citizen Kane movie 1941 119 369257 8.3 Orson Welles 1915 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Double Indemnity movie 1944 107 130903 8.3 Billy Wilder 1906 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Bicycle Thieves movie 1948 89 130810 8.3 Vittorio De Sica 1901 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Singin’ in the Rain movie 1952 103 200992 8.3 Stanley Donen 1924 FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
Vertigo movie 1958 128 327671 8.3 Alfred Hitchcock 1899 FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
North by Northwest movie 1959 136 276715 8.3 Alfred Hitchcock 1899 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
The Apartment movie 1960 125 147551 8.3 Billy Wilder 1906 FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
Lawrence of Arabia movie 1962 228 247178 8.3 David Lean 1908 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
To Kill a Mockingbird movie 1962 129 276491 8.3 Robert Mulligan 1925 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
For a Few Dollars More movie 1965 132 211177 8.3 Sergio Leone 1929 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
2001: A Space Odyssey movie 1968 149 553153 8.3 Stanley Kubrick 1928 FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
A Clockwork Orange movie 1971 136 701839 8.3 Stanley Kubrick 1928 FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
The Sting movie 1973 129 224060 8.3 George Roy Hill 1921 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Taxi Driver movie 1976 114 650939 8.3 Martin Scorsese 1942 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Das Boot movie 1981 149 214202 8.3 Wolfgang Petersen 1941 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
Star Wars: Episode VI - Return of the Jedi movie 1983 131 876077 8.3 Richard Marquand 1937 FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
Scarface movie 1983 170 676833 8.3 Brian De Palma 1940 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Amadeus movie 1984 160 342609 8.3 Milos Forman 1932 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Full Metal Jacket movie 1987 116 621426 8.3 Stanley Kubrick 1928 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Reservoir Dogs movie 1992 99 845824 8.3 Quentin Tarantino 1963 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Braveheart movie 1995 178 903150 8.3 Mel Gibson 1956 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Toy Story movie 1995 81 815123 8.3 John Lasseter 1957 TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE

Cotton Farming example

The cotton farming example shown within the seminar is fairly straightforward in comparison to the other packages despite kable being a more technical option. This is partly due to us being able to manually create our row groupings using pack_rows() rather than requiring an existing grouping column. Therefore in this case we did not need to create an additional column beforehand or use an additional function to directly group the data before we even made a table.

This function requires you to provide a kable for the grouping and then to specify the first and last rows within this new grouping.

knitr::kable(A,
             "html",
             col.names = c("Sub-group", 
                           "Farm Area (Mean)",
                           "Farm Area (Median)", 
                           "Cotton Area (Mean)",
                           "Cotton Area (Median)",
                           "Cotton Area as % of Farm Area (Mean)",
                           "Cotton Area (Median)",
                           "N"),
             caption = "Farm Area (Acre), Cotton Area (Acre), Cotton Area as % of Farm Area")%>%
  kable_styling(font_size = 9)%>%
  pack_rows(group_label = "State", 1, 4)%>% # include a label, the first and last rows of the grouping
  pack_rows(group_label = "Partner",5,11)%>%
  pack_rows(group_label = "Overall",12,12)
Farm Area (Acre), Cotton Area (Acre), Cotton Area as % of Farm Area
Sub-group Farm Area (Mean) Farm Area (Median) Cotton Area (Mean) Cotton Area (Median) Cotton Area as % of Farm Area (Mean) Cotton Area (Median) N
State
Gujarat 4.97 5.00 4.72 4.50 95.3 100.0 100
Madhya Pradesh 4.24 4.00 3.32 3.00 77.9 83.8 100
Maharashtra 2.12 1.00 2.04 1.00 98.5 100.0 100
Rajasthan 4.80 4.30 2.01 1.50 43.1 48.2 50
Partner
1 5.31 5.05 4.87 5.00 91.6 100.0 50
2 4.64 4.05 4.57 4.00 99.0 100.0 50
3 1.00 1.00 1.00 1.00 100.0 100.0 50
4 3.24 3.00 3.08 2.25 97.1 100.0 50
5 4.69 5.05 3.91 3.00 82.9 100.0 50
6 3.79 4.00 2.72 3.00 72.9 75.0 50
7 4.80 4.30 2.01 1.50 43.1 48.2 50
Overall
Total 3.92 4.00 3.16 3.00 83.8 100.0 350

Modelling Examples

For creating model tables, again the solution is to create a tidy model data frame using the broom package and then using kable on this data frame. You can edit this table in any way you wish. One extra thing to note is that you can use the code opts <- options(knitr.kable.NA = "") to specify an empty string for missing data instead of “NA”.

movies <- imdb%>%
  filter(type == "movie")%>%
  mutate(num_Votes_10000 = numVotes/10000)

model1 <- aov(averageRating ~ length + num_Votes_10000 + animation, movies)

m1 <- broom::tidy(model1) # create a tidy model table

opts <- options(knitr.kable.NA = "") # set NA values to an empty string

kable(m1,
      "html",
      digits = c(0,0,0,0,1,5))%>%
  kable_styling()
term df sumsq meansq statistic p.value
length 1 3710 3710 2834.6 0
num_Votes_10000 1 1170 1170 893.8 0
animation 1 339 339 259.0 0
Residuals 42278 55331 1
lm(averageRating ~ length + num_Votes_10000 + animation, movies) %>%
  broom::tidy()%>%
  kable("html", 
        digits = c(0,3,3,5))%>%
  kable_styling()
term estimate std.error statistic p.value
(Intercept) 4.925 0.026 187.77456 0
length 0.012 0.000 49.99840 0
num_Votes_10000 0.023 0.001 28.85666 0
animationTRUE 0.544 0.034 16.09431 0