SOCI832: Univariate Statistics in R

Lesson 2.3: R Commands for Univariate Statistics

Learning Objectives

By the end of this class, students should be able to run (with the assistance of, and ability to constantly refer to methods101.com and the internet (e.g. Google Search) and R Help) the following R commands:

  • set up commands
  • %>% - piping
  • select()
  • descr() from summarytools package
  • print()


1. Always run this

if(!require(dplyr)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjlabelled)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjmisc)) {install.packages("sjmisc", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjPlot)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(summarytools)) {install.packages("summarytools", repos='https://cran.csiro.au/', dependencies=TRUE)}
library(dplyr)
library(sjlabelled)
library(sjmisc)
library(sjPlot)
library(summarytools)

options(digits=5, scipen=15) # turns scientific notations (e.g., 2e-16) into numbers (e.g. 0.000000000002)

2. Piping with %>% from magrittr, important with dplyr

# y <- f(x(z(k(a))))

# a is a variable

# y <- a %>%
#  k() %>% 
#  z() %>% 
#  x() %>% 
#  f()

3. select() from dplyr package

  select(RESPID,
         SUICIDE2, SUICIDE3, SUICIDE4, SUICIDE9, SUICID11, SUICID12,
         STRESS, STRSSECO, STRSSJOB, STRSSFAM,
         ATTEND, RELIG, RELAFFCT,
         SEX, AGE, MARITAL, EMPLY, EDUC, INCOM0, SATFACE6)

4. summarytools::descr()

summarytools manual (.pdf)

  descr(omit.headings = TRUE,
        stats = c("mean", "sd", "min", "max"), 
        transpose = TRUE)