Not Preface

R is now a large community. Today we have 4,567 6,139 R packages. My attempt here is to jot down the code snippets based on the newer packages (by following table of available packages, sorted by date of publication)1.


The chunk of the codes will be taken either from the Vignette or from the main package documentation pdf. I also like to include stack overflow threads if there are interesting questions. These interesting and easy-to-apply codes will be applied to different data sets to develop models and assumptions.

Packages

packages used in this post are:

pander, DT, data.table

pander

  1. Package pdf.
  2. Github
  3. Last updated: 2014-XX-XX
library(pander)
pandoc.table(mtcars[1:3, 1:4])
## 
## -------------------------------------------
##                 mpg   cyl   disp   hp 
## ------------------- ----- ----- ------ ----
##    **Mazda RX4**     21     6    160   110 
## 
##  **Mazda RX4 Wag**   21     6    160   110 
## 
##   **Datsun 710**    22.8    4    108    93 
## -------------------------------------------
  mpg cyl disp hp
Mazda RX4 21 6 160 110
Mazda RX4 Wag 21 6 160 110
Datsun 710 22.8 4 108 93
pandoc.table(mtcars[1:2, ], style = "grid", caption = "Hello caption!")
## 
## 
## +---------------------+-------+-------+--------+------+--------+-------+
## |                |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt   |
## +=====================+=======+=======+========+======+========+=======+
## |    **Mazda RX4**    |  21   |   6   |  160   | 110  |  3.9   | 2.62  |
## +---------------------+-------+-------+--------+------+--------+-------+
## |  **Mazda RX4 Wag**  |  21   |   6   |  160   | 110  |  3.9   | 2.875 |
## +---------------------+-------+-------+--------+------+--------+-------+
## 
## Table: Hello caption! (continued below)
## 
##  
## 
## +---------------------+--------+------+------+--------+--------+
## |                |  qsec  |  vs  |  am  |  gear  |  carb  |
## +=====================+========+======+======+========+========+
## |    **Mazda RX4**    | 16.46  |  0   |  1   |   4    |   4    |
## +---------------------+--------+------+------+--------+--------+
## |  **Mazda RX4 Wag**  | 17.02  |  0   |  1   |   4    |   4    |
## +---------------------+--------+------+------+--------+--------+
Hello caption! (continued below)
  mpg cyl disp hp drat wt

Mazda RX4

21

6

160

110

3.9

2.62

Mazda RX4 Wag

21

6

160

110

3.9

2.875

  qsec vs am gear carb

Mazda RX4

16.46

0

1

4

4

Mazda RX4 Wag

17.02

0

1

4

4

pander(prcomp(USArrests))
Principal Components Analysis
  PC1 PC2 PC3 PC4
Murder 0.0417 -0.04482 0.07989 -0.9949
Assault 0.9952 -0.05876 -0.06757 0.03894
UrbanPop 0.04634 0.9769 -0.2005 -0.05817
Rape 0.07516 0.2007 0.9741 0.07233
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
m <- glm(counts ~ outcome + treatment, family = poisson())
pander(m)
Fitting generalized (poisson/log) linear model: counts ~ outcome + treatment
  Estimate Std. Error z value Pr(>|z|)
outcome2 -0.4543 0.2022 -2.247 0.02465
outcome3 -0.293 0.1927 -1.52 0.1285
treatment2 1.338e-15 0.2 6.69e-15 1
treatment3 1.421e-15 0.2 7.105e-15 1
(Intercept) 3.045 0.1709 17.81 5.427e-71
Fitting generalized (poisson/log) linear model: counts ~ outcome + treatment
  Estimate Std. Error z value Pr(>|z|)
outcome2 -0.4543 0.2022 -2.247 0.02465
outcome3 -0.293 0.1927 -1.52 0.1285
treatment2 1.338e-15 0.2 6.69e-15 1
treatment3 1.421e-15 0.2 7.105e-15 1
(Intercept) 3.045 0.1709 17.81 5.427e-71
pander(aov(m))
Analysis of Variance Model
  Df Sum Sq Mean Sq F value Pr(>F)
outcome 2 92.67 46.33 2.224 0.2242
treatment 2 8.382e-31 4.191e-31 2.012e-32 1
Residuals 4 83.33 20.83 NA NA
library(pander)
pandoc.table(head(mtcars), split.tables=Inf, style='rmarkdown')
## 
## 
## |         &nbsp;          |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt   |  qsec  |  vs  |  am  |  gear  |  carb  |
## |:-----------------------:|:-----:|:-----:|:------:|:----:|:------:|:-----:|:------:|:----:|:----:|:------:|:------:|
## |      **Mazda RX4**      |  21   |   6   |  160   | 110  |  3.9   | 2.62  | 16.46  |  0   |  1   |   4    |   4    |
## |    **Mazda RX4 Wag**    |  21   |   6   |  160   | 110  |  3.9   | 2.875 | 17.02  |  0   |  1   |   4    |   4    |
## |     **Datsun 710**      | 22.8  |   4   |  108   |  93  |  3.85  | 2.32  | 18.61  |  1   |  1   |   4    |   1    |
## |   **Hornet 4 Drive**    | 21.4  |   6   |  258   | 110  |  3.08  | 3.215 | 19.44  |  1   |  0   |   3    |   1    |
## |  **Hornet Sportabout**  | 18.7  |   8   |  360   | 175  |  3.15  | 3.44  | 17.02  |  0   |  0   |   3    |   2    |
## |       **Valiant**       | 18.1  |   6   |  225   | 105  |  2.76  | 3.46  | 20.22  |  1   |  0   |   3    |   1    |
  mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
name size nnwe.have.dates
a 1 2015-01-01
b 2 2014-12-31
c 3 2014-12-30
name size nnwe.have.dates
a 1 2014-12-30
b 2 2014-12-29
c 3 2014-12-28
mytab = data.frame(col1=1:2, col2=2001:2002, col3="This is a lengthy test that should wrap, and wrap again, and again and again and again")
pandoc.table(mytab)
## 
## -------------------------------------------
##   col1   col2 col3                         
## ------ ------ -----------------------------
##      1   2001 This is a lengthy test that  
##               should wrap, and wrap again, 
##               and again and again and again
## 
##      2   2002 This is a lengthy test that  
##               should wrap, and wrap again, 
##               and again and again and again
## -------------------------------------------
col1 col2 col3
1 2001 This is a lengthy test that should wrap, and wrap again, and again and again and again
2 2002 This is a lengthy test that should wrap, and wrap again, and again and again and again

DT

  1. jQuery DataTables.
  2. DT Github Repo.
  3. Rpubs 1: knitr と DataTables
  4. Rpubs 2: countrycode

DT package is not on CRAN yet, and you can install it with devtools. Need to do following:

install.packages(“devtools”)

library(devtools)

devtools::install_github(‘rstudio/DT’)

### TWITTER TRENDS on DEC 23, 2014
library(DT)
data <- read.csv("aa.csv")
datatable(data)

data.table

  1. Package pdf.
  2. DATA ANALYSIS THE DATA.TABLE WAY
library(data.table)
DF = data.frame(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DF
##   x y v
## 1 a 1 1
## 2 a 3 2
## 3 a 6 3
## 4 b 1 4
## 5 b 3 5
## 6 b 6 6
## 7 c 1 7
## 8 c 3 8
## 9 c 6 9
DT
##    x y v
## 1: a 1 1
## 2: a 3 2
## 3: a 6 3
## 4: b 1 4
## 5: b 3 5
## 6: b 6 6
## 7: c 1 7
## 8: c 3 8
## 9: c 6 9
data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
mtcars.dt <- data.table(mtcars)
head(mtcars.dt)
##     mpg cyl disp  hp drat    wt  qsec vs am gear carb
## 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## 4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## 5: 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## 6: 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
mtcars.dt1 <- data.table(mtcars, keep.rownames = TRUE)
head(mtcars.dt1)
##                   rn  mpg cyl disp  hp drat    wt  qsec vs am gear carb
## 1:         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## 2:     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## 3:        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## 4:    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## 5: Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## 6:           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
trend <- data.table(read.csv("aa.csv"))
head(trend)
##    X                      name
## 1: 1              #1DTVSpecial
## 2: 2          #shanedawsonsony
## 3: 3       #HappyBirthdayLouis
## 4: 4                #2014Faves
## 5: 5 Tomorrow is Christmas Eve
## 6: 6  #23ReasonsWhyWeLoveLouis
##                                                            url
## 1:                  http://twitter.com/search?q=%231DTVSpecial
## 2:              http://twitter.com/search?q=%23shanedawsonsony
## 3:           http://twitter.com/search?q=%23HappyBirthdayLouis
## 4:                    http://twitter.com/search?q=%232014Faves
## 5: http://twitter.com/search?q=%22Tomorrow+is+Christmas+Eve%22
## 6:      http://twitter.com/search?q=%2323ReasonsWhyWeLoveLouis
##                              query    woeid
## 1:                  %231DTVSpecial 23424977
## 2:              %23shanedawsonsony 23424977
## 3:           %23HappyBirthdayLouis 23424977
## 4:                    %232014Faves 23424977
## 5: %22Tomorrow+is+Christmas+Eve%22 23424977
## 6:      %2323ReasonsWhyWeLoveLouis 23424977

  1. Compiled by Subasish Das