knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = FALSE)
This vignette relies on the following packages and a custom dataset.
library('lehmansociology') library(dplyr) library(xtable) library(printr) education_and_poverty<-create_educ_poverty_data()
There are many kinds of "tables" that we make in presenting statistical data and doing statistical analysis. A table can be anything that is organized into columns and rows.
For example you could consider presenting the 5 number summary for a variable to be a kind of table.
fivenum(poverty.states$PCTPOVALL_2013)
More commonly in sociology when we say table we refer to a frequency, proportion or percent table.
table(education_and_poverty$region) # You can display this as proportions. Notice how the function builds from the inside out. prop.table(table(education_and_poverty$region)) # Alternatively you could do this mytable <- table(education_and_poverty$region) prop.table(mytable) # You can make the display show percents instead of proportions and round to one decimal. round(prop.table(mytable)*100, 1) # You can make it display vertically by converting the table back to a dataframe. as.data.frame(mytable)
If we switch to a variable that is ordinal or interval we can create a cumulative percent table
cumulativetvtable <- cumsum(prop.table(table(addhealth$tvhrs)))*100 cumulativetvtable <- as.data.frame(cumulativetvtable) round(cumulativetvtable, 0)
Now we will do acomparison (of means and medians) for different groups with a table of values.
There are many ways to do this, but this way uses some functions from the dplyr package and the magritte operator (%>%).
#start with your dataset education_and_poverty %>% #group it according the the variable you want group_by(region) %>% # Use summarize to define the table columns # You can add any othe statistics you want, such as max() or min() # Add the new statistics to the list inside the parentheses. summarize( 'Mean poverty' = mean(PCTPOVALL_2013), 'Median poverty' = median(PCTPOVALL_2013), 'IQR poverty' = IQR(PCTPOVALL_2013) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.