[This article was first published on Coffee and Econometrics in the Morning, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Obtaining a coherent numerical summary of data is a common task, and it is common to want to port these summary statistics into a table of results. When I am in interactive mode with my data, I use the summary() command applied to my data frame. For example, the following code loads and summarizes a data frame on Yogurt advertising and prices:

library(Ecdat) ## Econometrics Data (useful!)
data(Yogurt) ## Loads Yogurt from Ecdat
summary(Yogurt) ## Summarizes Yogurt

For each quantitative variable, the summary() command provides a five-number summary (min, max, Q1, Q3, median) plus the mean. For categorical variables, the counts of each level are provided. This provides an excellent summary measure of each variable, but you may prefer a richer set of information (especially when it comes to typing up tables).

I recently discovered a great way to obtain a richer set of information on a data frame. This method involves using the psych library, which contains functions describe() and describe.by(). Continuing with the code from above, here is the basic syntax:

library(psych)
describe(Yogurt) ## Describes in more detail the Yogurt data frame

Suppose you also want to break your summary statistics into two (or four) tables for comparison sake (perhaps to illustrate stark differences across select subsets of your data). The describe.by() command is a convenient technique to break the data down by the levels of a factor. Here’s an example with on the Yogurt data.

describe.by(Yogurt, Yogurt\$choice)

Finally, you may want to port your data into LaTeX format and/or select particular summary statistics from the list. I wrote a function that serves as a convenience interface to describe.by() and toLatex(). As toLatex() does not work directly on objects created using describe.by(), you might find this helpful.

If you do not like knowing about the kurtosis of your data, you could read up on the options of describe.by() to learn about how to shut it down. If you’re going to port it into a LaTeX table anyway, you could also just modify the code I wrote here to eliminate the summary statistics you don’t want and produce LaTeX output.

FYI: Quick R has a nice summary of some other methods for summarizing data. Of the methods at Quick R that I didn’t describe, pastecs looks most like a method I would use.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8796429,"math_prob":0.42020327,"size":3125,"snap":"2022-40-2023-06","text_gpt3_token_len":688,"char_repetition_ratio":0.1201538,"word_repetition_ratio":0.09423077,"special_character_ratio":0.21152,"punctuation_ratio":0.10294118,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9610104,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-02T02:42:07Z\",\"WARC-Record-ID\":\"<urn:uuid:ba085ab1-019f-41c2-b47d-9b7a256aab9b>\",\"Content-Length\":\"88589\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:df442b8c-f920-412a-898a-5645db695467>\",\"WARC-Concurrent-To\":\"<urn:uuid:87576c87-37ea-48d5-b979-a6f4482f778e>\",\"WARC-IP-Address\":\"172.67.211.236\",\"WARC-Target-URI\":\"https://www.r-bloggers.com/2011/05/describing-data-frequently-used-commands/\",\"WARC-Payload-Digest\":\"sha1:72W3A557GSLUQP262V4RLTXVDIADZOOC\",\"WARC-Block-Digest\":\"sha1:QGK6BATBOWNRREJ6DTF254TI7Y4KQB3D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499954.21_warc_CC-MAIN-20230202003408-20230202033408-00073.warc.gz\"}"}