Get access to all lessons in this course.
-
Advanced Data Wrangling and Analysis
- Overview
- Importing Data
- Tidy Data
- Reshaping Data
- Dealing with Missing Data
- Changing Variable Types
- Advanced Variable Creation
- Advanced Summarizing
- Binding Data Frames
- Functions
- Merging Data
- Renaming Variables
- Quick Interlude to Reorganize our Code
- Exporting Data
-
Advanced Data Visualization
- Data Visualization Best Practices
- Tidy Data
- Pipe Data Into ggplot
- Reorder Plots to Highlight Findings
- Line Charts
- Use Color to Highlight Findings
- Declutter
- Use the scales Package for Nicely Formatted Values
- Use Direct Labeling
- Use Axis Text Wisely
- Use Titles to Highlight Findings
- Use Color in Titles to Highlight Findings
- Use Annotations to Explain
- Tweak Spacing
- Customize Your Theme
- Customize Your Fonts
- Try New Plot Types
-
Advanced RMarkdown
- Advanced Markdown Text Formatting
- Tables
- Advanced YAML
- Inline R Code
- Making Your Reports Shine: Word Edition
- Making Your Reports Shine: HTML Edition
- Making Your Reports Shine: PDF Edition
- Presentations
- Dashboards
- Other Formats
-
Wrapping Up
- You Did It!
Going Deeper with R
Tables
This lesson is locked
This lesson is called Tables, part of the Going Deeper with R course. This lesson is called Tables, part of the Going Deeper with R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Change the default data frame printing method to use kable
Choose one of the table packages and make an attractive table that shows the top 10 districts with the highest percentage of Hispanic/Latino students (hint: use the
slice_max()
function to get the top 10 and thefmt_percent()
function if you’re usinggt
or thepercent()
function otherwise to display the percentage of Hispanic/Latino students in each)
Please note that in order to use the slice_max()
function, you need to have dplyr
1.0 or greater installed.
Learn More
If you want to read more about df_print options, you can find that here.
If you’re looking for an overview of all of the table packages discussed in this lesson, check out this comprehensive blog post I wrote.
That blog post includes links to some great tutorials on the various packages. Some additional tutorials have come out since then, especially on the gt
package. See, for example, tutorials by:
In case it’s not already clear, the gt
package is really popular, in large part because of this endorsement from Hadley Wickham.
gt = grammar of tables. It aims to do for tables what ggplot2 did for graphics. It’s still early days and tables are surprisingly complicated, but this is a very exciting package by a skilled developer! #rstats https://t.co/138FrCy5th
— Hadley Wickham (@hadleywickham) April 8, 2020
You need to be signed-in to comment on this post. Login.
Abby Isaacson
May 25, 2021
I'm not sure at what step my years got off, but my enrollment_by_race_ethnicity dataset has always displayed '2018-2019' for one year and '2017-18' format for the other. I tried using col_labels to rename a numeric variable but it didn't work. Can I rename in gt, or do I have to go back to the dataset?
David Keyes
May 26, 2021
You can do it either way. If you want to rename in gt, you'd use the
cols_label()
function.Abby Isaacson
May 26, 2021
Thanks! I was missing the
(back tick) around the original number. So cols_label(
2017-18` = "2017-2018") worked.David Keyes
May 26, 2021
Glad you got it figured out!
Jessica Sickler
May 26, 2021
Is it possible to print a formatted table as an image to insert in report or document outside of R?
David Keyes
May 26, 2021
Yep! You can do this with the
gtsave()
function from thegt
package. Thesave_as_image()
function fromflextable
does the same thing. I'm not sure if the other packages do something similar because I haven't used them as much.Jody Oconnor
May 29, 2021
I'd like to create the 'your turn' table using flextable, but I'm not having much luck with the syntax. Could you post example code using flextable instead of gt please? Thanks!
David Keyes
June 1, 2021
Yup! I'll do this soon. I'm moving tomorrow but should be able to do it later this week.
David Keyes
June 4, 2021
I finally got around to doing this. You can see a video here and my code here. Please let me know if you have any questions!
Jody Oconnor
June 7, 2021
Thanks! I think I like gt better (flextable seems to require more code to do the same thing and is less intuitive), but I want to be able to knit to word. The one thing I can't seem to do is format a title in flextable (it lets me set a caption at the top, but as far as I can tell the formatting can only be changed through the Word document's style with officer::styles_info). Is that true or am I missing something? If it is true, I'd be happy to see an example of how to make styles_info work if you have time... the documentation is a bit confusing.
Also, just so you know, your video doesn't show your R studio window after it spontaneously closed and you re-opened it. No need to re-record it though since you posted the code.
David Keyes
June 9, 2021
Hmm, I don't actually know the answer to your question on titles, but I think it probably does use the Word styles for titles. You could use Word reference docs to style those. Another option I see from a quick perusal of the docs is this: https://davidgohel.github.io/flextable/reference/add_header_lines.html. Looks like you could add either a title or a caption with it.
Atlang Mompe
June 25, 2021
Hi David, it seems that I cannot load the gt library - I have tried to follow your video? Please advise. > library(gt) Error in library(gt) : there is no package called ‘gt’
Atlang Mompe
June 25, 2021
OMG I figured it out - this may be a basic question David but sometimes when I try to load a library and it refuses, if I step away from the computer, I usually find an option to install, what prompts this, I know we most probably covered this in our earlier classes but moving forward, is it best to install the package - from packages before loading the library - sorry for the basic question - we have covered a lot in the past 3 months.
David Keyes
June 25, 2021
Hmm, hard to know exactly without seeing your computer. Do you get a specific error when you try to install a package and it fails to do so?
Andrew Paquin
May 29, 2023
Hi David, As I worked through this stuff today, I received several warnings that "vars" has been deprecated and that I should used c() instead. Has vars gone the way of the pet rock? Is there a hard date after which it simple won't work at all?
David Keyes
May 30, 2023
I didn't actually know this change happened! I did, however, just look at the changelog. It does seem like vars() will be going away eventually so probably best to use c() instead.