knitr::opts_chunk$set(echo = TRUE,comment = NA,message=FALSE,warning=FALSE,fig.width=6,fig.height = 6, fig.align='center',out.width="70%")

You can make tables summarizing descriptive statistics easily with webr package.

Installation of packages

You have to install the latest versions of "webr" and "moonBook" packages from github.

if(!require(devtools)) install.packages("devtools")
devtools::install_github("cardiomoon/webr")
devtools::install_github("cardiomoon/moonBook")   # For examples
devtools::install_github("cardiomoon/rrtable")    # For reproducible research

Load packages

require(webr)
require(moonBook) # For data acs

Summarizing Frequencies

You can summmarize the frequencies easily with freqSummary() function. Also you can make a table summarizng frequencies with freqTable() function.

freqSummary(acs$Dx)
freqTable(acs$Dx)

Ready for reproducible research

The freqTable() function returns an object of class "flextable". With this object, you can make html, pdf, docx, pptx file easily.

result=freqTable(acs$Dx)
class(result)

Frequency table for a continuous variable

You can make the frequency table for a continuous variable. In this time, you can get a long table.

freqTable(mtcars$mpg)

Frequency table for two categorical variables

You can make a table summarizing the independency of two categorical variables.

x2Table(acs,Dx,sex)

You can make a table with columnwise percentages.

x2Table(acs,Dx,sex,margin=2)

You can hide pecentages.

x2Table(acs,Dx,sex,show.percent=FALSE)

Numerical summary

Numerical summary of a vector

You can make a numerical summary table with numSummary() function. If you use the numSummary() function to a continuous vector, you can get the following summary. This function uses psych::describe function

require(dplyr)
numSummary(acs$age)
numSummaryTable(acs$age)

Numerical summary of a data.frame or a tibble

You can make a numerical summary of a data.frame. The numSummary function uses is.numeric function to select numeric columns and make a numeric summary.

numSummary(acs)
numSummaryTable(acs)

Use of dplyr::group_by() and dplyr::select() function to summarize

You can use dplyr::select() function to select variables to summarize.

acs %>% select(age,EF) %>% numSummary
acs %>% select(age,EF) %>% numSummaryTable

You can use dplyr::group_by() and dplyr::select() function to select variables to summarize by group.

acs %>% group_by(sex) %>% select(age,EF) %>% numSummary
acs %>% group_by(sex) %>% select(age,EF) %>% numSummaryTable

You can summarize by multiple groups.

acs %>% group_by(sex,Dx) %>% select(age,EF) %>% numSummary
acs %>% group_by(sex,Dx) %>% select(age,EF) %>% numSummaryTable

For reproducible research

You can use package rrtable for reproducible research.

require(rrtable)
type=c("table","table")
title=c("Frequency Table","Numerical Summary")
code=c("freqTable(acs$Dx)","acs %>% group_by(sex) %>% select(EF,age) %>% numSummaryTable")
data=data.frame(type,title,code,stringsAsFactors = FALSE)
data2pptx(data)
data2docx(data)


cardiomoon/webr documentation built on May 12, 2020, 4:12 a.m.