%>%
costofliving summarise(cofi_mean = mean(cofi, na.rm = TRUE),
cofi_sd = sd(cofi, na.rm = TRUE))
# A tibble: 1 × 2
cofi_mean cofi_sd
<dbl> <dbl>
1 58.7 21.0
Sometimes, we might want to display several statistical measures at the same time. We can display the mean and the standard deviation together using summarise
.
%>%
costofliving summarise(cofi_mean = mean(cofi, na.rm = TRUE),
cofi_sd = sd(cofi, na.rm = TRUE))
# A tibble: 1 × 2
cofi_mean cofi_sd
<dbl> <dbl>
1 58.7 21.0
And we can combine several tidyverse
functions such as filter
, select
, group_by
, summarise
and rename
to get a table showing some central tendencies and variation on variables and units that are important for our analysis.
<- costofliving %>%
cofi_table filter(country %in% c("United Kingdom", "Germany", "China", "United States")) %>% # Picking out these countries
select(country, cofi) %>% # Fetching only the variables select and cofi
group_by(country) %>% # Finding the next summary statistics per country
summarise(cofi_mean = mean(cofi, na.rm = TRUE), # Finding the mean of cost of living (per country)
cofi_sd = sd(cofi, na.rm = TRUE)) %>% # Finding the standard deviation of cost of living (per country)
rename("Country" = country, # Renaming the variables into something more human readable
"Average cost of living" = cofi_mean,
"Spread around cost of living" = cofi_sd)
cofi_table
# A tibble: 4 × 3
Country `Average cost of living` `Spread around cost of living`
<chr> <dbl> <dbl>
1 China 48.7 6.72
2 Germany 65.9 2.95
3 United Kingdom 71.0 3.73
4 United States 72.9 9.18
If we would like to include this table in a pdf
or html
report, we can use the knitr
package and the kableExtra
package to wrap the table into a neat table-structure. The base functions in these packages is kable
to make a pdf- or html-table, and kable_styling
to make it a bit more beautiful. Here, I add the argument latex_options = "HOLD_position"
because it stops the table from jumping around in the final pdf file.
library(knitr)
library(kableExtra)
%>%
cofi_table kable() %>% # Make html or pdf table
kable_styling(latex_options = "HOLD_position") # Make sure that the table stays in place
Country | Average cost of living | Spread around cost of living |
---|---|---|
China | 48.69667 | 6.719839 |
Germany | 65.90250 | 2.946786 |
United Kingdom | 71.01500 | 3.732458 |
United States | 72.89743 | 9.178107 |
There are many ways to style your table so that it becomes the way you want it to, for example as shown in the code below. If you want to learn more, have a look at this page. Here, I also round the variable to two decimals before I make a beautiful table out of them, using mutate
and round
.
%>%
cofi_table mutate(`Average cost of living` = round(`Average cost of living`, 2), # Rounding the variable into two decimals
`Spread around cost of living` = round(`Spread around cost of living`, 2)) %>%
kable() %>%
kable_classic(full_width = FALSE, # Make the table fill the page with theme "classic"
html_font = "Cambria", # Give a certain font to the page
font_size = 12, # Set font size to 12
latex_options = "HOLD_position") %>% # Keep the table in this spot when making the pdf file
row_spec(0, color = "blue") %>% # Make the text in the top row blue
column_spec(1, bold = TRUE, # Set the first column to bold
border_right = TRUE) # Add a vertical line at the first column
Country | Average cost of living | Spread around cost of living |
---|---|---|
China | 48.70 | 6.72 |
Germany | 65.90 | 2.95 |
United Kingdom | 71.02 | 3.73 |
United States | 72.90 | 9.18 |