Last updated: 2019-09-20

Checks: 7 0

Knit directory: STA_463_563_Fall2019/

This reproducible R Markdown analysis was created with workflowr (version 1.4.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20190905) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd f1923b8 dleelab 2019-09-20 c
html 21682d9 dleelab 2019-09-13 pipe moved
Rmd 420b3f5 dleelab 2019-09-13 pipe moved
html 8fe2e84 dleelab 2019-09-13 add
Rmd 95b04a6 dleelab 2019-09-13 added
Rmd 5f075a3 dleelab 2019-09-13 repo updated
html 69adea1 statslee 2019-09-06 html built
Rmd 5c642d9 statslee 2019-09-06 created


An Intro to R data programming

Base R tools:
* classes
* numeric summaries
* basic plots

New R tools:
* tidyverse (is a collection of R packages)
* ggplot2 package: advanced graphics
* dplyr package: data manipulation, working with data frames

Part 1: Object Type

icecream <- read.table("data/icecream.txt")
dim(icecream)
[1] 200   5
copier <- read.table("data/CH01PR20.txt")
dim(copier)
[1] 45  2

Understand Dataframe

class(icecream)
[1] "data.frame"
class(copier)
[1] "data.frame"
head(icecream)
   id female ice_cream video puzzle
1  70      0         2    47     57
2 121      1         1    63     61
3  86      0         3    58     31
4 141      0         3    53     56
5 172      0         1    53     61
6 113      0         1    63     61
head(copier)
   V1 V2
1  20  2
2  60  4
3  46  3
4  41  2
5  12  1
6 137 10
colnames(copier)=c("minutes","number")
head(copier)
  minutes number
1      20      2
2      60      4
3      46      3
4      41      2
5      12      1
6     137     10
#alternative way
copier <- setNames(copier,c("minutes","number"))
head(copier)
  minutes number
1      20      2
2      60      4
3      46      3
4      41      2
5      12      1
6     137     10
dim(icecream)
[1] 200   5
names(icecream)
[1] "id"        "female"    "ice_cream" "video"     "puzzle"   
class(icecream$video)
[1] "integer"
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
class(iris$Species)
[1] "factor"
class(iris$Sepal.Length)
[1] "numeric"

Class type can be changed

class(icecream$video)
[1] "integer"
x=as.numeric(icecream$video)
class(x)
[1] "numeric"

Check the difference of the following to different types

x
  [1] 47 63 58 53 53 63 53 39 58 50 53 63 61 55 31 50 50 58 55 53 66 72 55
 [24] 61 39 39 61 58 39 55 47 64 66 72 61 61 66 66 36 39 42 58 55 50 63 69
 [47] 49 63 53 47 57 47 50 55 69 26 33 56 58 44 58 69 34 36 36 50 55 42 65
 [70] 44 39 58 63 74 58 45 49 63 39 42 55 61 66 63 44 63 53 42 34 61 47 66
 [93] 69 44 47 63 66 69 39 61 69 66 33 50 61 42 50 51 50 58 61 39 46 59 55
[116] 42 55 58 58 39 50 50 39 48 34 58 44 50 47 29 50 54 50 47 44 67 58 44
[139] 42 44 44 50 39 44 53 48 55 44 40 34 42 58 50 53 58 55 54 47 42 61 53
[162] 51 63 61 55 40 61 47 55 53 50 47 31 61 35 54 55 53 58 56 50 39 63 50
[185] 66 58 53 42 55 53 42 50 55 34 50 42 36 55 58 53
y=as.factor(x)
class(y)
[1] "factor"
y
  [1] 47 63 58 53 53 63 53 39 58 50 53 63 61 55 31 50 50 58 55 53 66 72 55
 [24] 61 39 39 61 58 39 55 47 64 66 72 61 61 66 66 36 39 42 58 55 50 63 69
 [47] 49 63 53 47 57 47 50 55 69 26 33 56 58 44 58 69 34 36 36 50 55 42 65
 [70] 44 39 58 63 74 58 45 49 63 39 42 55 61 66 63 44 63 53 42 34 61 47 66
 [93] 69 44 47 63 66 69 39 61 69 66 33 50 61 42 50 51 50 58 61 39 46 59 55
[116] 42 55 58 58 39 50 50 39 48 34 58 44 50 47 29 50 54 50 47 44 67 58 44
[139] 42 44 44 50 39 44 53 48 55 44 40 34 42 58 50 53 58 55 54 47 42 61 53
[162] 51 63 61 55 40 61 47 55 53 50 47 31 61 35 54 55 53 58 56 50 39 63 50
[185] 66 58 53 42 55 53 42 50 55 34 50 42 36 55 58 53
34 Levels: 26 29 31 33 34 35 36 39 40 42 44 45 46 47 48 49 50 51 53 ... 74
z=as.character(x)
class(z)
[1] "character"
z
  [1] "47" "63" "58" "53" "53" "63" "53" "39" "58" "50" "53" "63" "61" "55"
 [15] "31" "50" "50" "58" "55" "53" "66" "72" "55" "61" "39" "39" "61" "58"
 [29] "39" "55" "47" "64" "66" "72" "61" "61" "66" "66" "36" "39" "42" "58"
 [43] "55" "50" "63" "69" "49" "63" "53" "47" "57" "47" "50" "55" "69" "26"
 [57] "33" "56" "58" "44" "58" "69" "34" "36" "36" "50" "55" "42" "65" "44"
 [71] "39" "58" "63" "74" "58" "45" "49" "63" "39" "42" "55" "61" "66" "63"
 [85] "44" "63" "53" "42" "34" "61" "47" "66" "69" "44" "47" "63" "66" "69"
 [99] "39" "61" "69" "66" "33" "50" "61" "42" "50" "51" "50" "58" "61" "39"
[113] "46" "59" "55" "42" "55" "58" "58" "39" "50" "50" "39" "48" "34" "58"
[127] "44" "50" "47" "29" "50" "54" "50" "47" "44" "67" "58" "44" "42" "44"
[141] "44" "50" "39" "44" "53" "48" "55" "44" "40" "34" "42" "58" "50" "53"
[155] "58" "55" "54" "47" "42" "61" "53" "51" "63" "61" "55" "40" "61" "47"
[169] "55" "53" "50" "47" "31" "61" "35" "54" "55" "53" "58" "56" "50" "39"
[183] "63" "50" "66" "58" "53" "42" "55" "53" "42" "50" "55" "34" "50" "42"
[197] "36" "55" "58" "53"

Change the variable ice_cream to factor

icecream$ice_cream
  [1] 2 1 3 3 1 1 1 1 1 1 1 1 3 3 2 2 3 1 3 1 1 1 1 3 1 1 3 1 3 2 1 3 3 1 3
 [36] 3 1 3 2 1 1 1 1 2 3 2 3 1 1 1 1 3 1 1 1 3 2 1 1 1 1 3 2 1 3 1 1 2 2 1
 [71] 1 3 1 1 1 2 3 3 1 3 3 2 1 2 1 3 3 3 1 3 1 2 3 2 2 2 1 3 2 3 3 3 2 3 1
[106] 1 3 2 2 3 1 2 3 2 3 1 3 3 2 2 2 1 1 1 2 3 1 1 3 2 1 2 1 1 2 3 1 1 1 1
[141] 2 3 1 1 1 3 1 1 2 2 2 3 1 1 1 1 1 1 1 3 2 1 3 2 1 2 1 2 1 2 2 1 3 3 2
[176] 2 1 1 3 2 1 1 3 1 3 2 1 1 3 2 2 3 1 3 1 1 1 1 1 3
class(icecream$ice_cream)
[1] "integer"
icecream$ice_cream=as.factor(icecream$ice_cream)
icecream$ice_cream
  [1] 2 1 3 3 1 1 1 1 1 1 1 1 3 3 2 2 3 1 3 1 1 1 1 3 1 1 3 1 3 2 1 3 3 1 3
 [36] 3 1 3 2 1 1 1 1 2 3 2 3 1 1 1 1 3 1 1 1 3 2 1 1 1 1 3 2 1 3 1 1 2 2 1
 [71] 1 3 1 1 1 2 3 3 1 3 3 2 1 2 1 3 3 3 1 3 1 2 3 2 2 2 1 3 2 3 3 3 2 3 1
[106] 1 3 2 2 3 1 2 3 2 3 1 3 3 2 2 2 1 1 1 2 3 1 1 3 2 1 2 1 1 2 3 1 1 1 1
[141] 2 3 1 1 1 3 1 1 2 2 2 3 1 1 1 1 1 1 1 3 2 1 3 2 1 2 1 2 1 2 2 1 3 3 2
[176] 2 1 1 3 2 1 1 3 1 3 2 1 1 3 2 2 3 1 3 1 1 1 1 1 3
Levels: 1 2 3
class(icecream$ice_cream)
[1] "factor"

Part 2: Exploratory Data Analysis

(1) Numerical summaries:
-mean, median, five number summary, standard deviation, IQR, correlation, etc.

Traditional R

mean(icecream$video)
[1] 51.85
median(icecream$video)
[1] 53
sd(icecream$video)
[1] 9.900891
var(icecream$video)
[1] 98.02764
summary(icecream$video)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  26.00   44.00   53.00   51.85   58.00   74.00 
IQR(icecream$video)
[1] 14
cor(copier$minutes,copier$number)
[1] 0.978517
summary(icecream)
       id             female      ice_cream     video      
 Min.   :  1.00   Min.   :0.000   1:95      Min.   :26.00  
 1st Qu.: 50.75   1st Qu.:0.000   2:47      1st Qu.:44.00  
 Median :100.50   Median :1.000   3:58      Median :53.00  
 Mean   :100.50   Mean   :0.545             Mean   :51.85  
 3rd Qu.:150.25   3rd Qu.:1.000             3rd Qu.:58.00  
 Max.   :200.00   Max.   :1.000             Max.   :74.00  
     puzzle     
 Min.   :26.00  
 1st Qu.:46.00  
 Median :52.00  
 Mean   :52.41  
 3rd Qu.:61.00  
 Max.   :71.00  
summary(copier)
    minutes           number      
 Min.   :  3.00   Min.   : 1.000  
 1st Qu.: 36.00   1st Qu.: 2.000  
 Median : 74.00   Median : 5.000  
 Mean   : 76.27   Mean   : 5.111  
 3rd Qu.:111.00   3rd Qu.: 7.000  
 Max.   :156.00   Max.   :10.000  

Advanced summary statistics (tidyverse)
“tibbles” instead of R’s traditional data.frame. Tibbles are data frames, but they tweak some older behaviours to make life a little easier.

Install and load R package “dplyr”

#install.packages("dplyr")
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

pipe
Use %>%(pipe). You can read it as a series of imperative statements: group, then summarise, then filter. A good way to pronounce %>% when reading code is “then”.Behind the scenes, x %>% f(y) turns into f(x, y), and x %>% f(y) %>% g(z) turns into g(f(x, y), z) and so on. You can use the pipe to rewrite multiple operations in a way that you can read left-to-right, top-to-bottom.

dplyr verbs
‘filter’,‘arrange’,‘mutate’,‘summarise’,‘group_by’

filter: select cases based on their values

head(icecream)
   id female ice_cream video puzzle
1  70      0         2    47     57
2 121      1         1    63     61
3  86      0         3    58     31
4 141      0         3    53     56
5 172      0         1    53     61
6 113      0         1    63     61
icecream <- as_tibble(icecream)
icecream
# A tibble: 200 x 5
      id female ice_cream video puzzle
   <int>  <int> <fct>     <int>  <int>
 1    70      0 2            47     57
 2   121      1 1            63     61
 3    86      0 3            58     31
 4   141      0 3            53     56
 5   172      0 1            53     61
 6   113      0 1            63     61
 7    50      0 1            53     61
 8    11      0 1            39     36
 9    84      0 1            58     51
10    48      0 1            50     51
# … with 190 more rows
icecream %>% filter(female==0)
# A tibble: 91 x 5
      id female ice_cream video puzzle
   <int>  <int> <fct>     <int>  <int>
 1    70      0 2            47     57
 2    86      0 3            58     31
 3   141      0 3            53     56
 4   172      0 1            53     61
 5   113      0 1            63     61
 6    50      0 1            53     61
 7    11      0 1            39     36
 8    84      0 1            58     51
 9    48      0 1            50     51
10    75      0 1            53     61
# … with 81 more rows
icecream %>% filter(female==1, video<50)
# A tibble: 41 x 5
      id female ice_cream video puzzle
   <int>  <int> <fct>     <int>  <int>
 1     8      1 2            44     48
 2   129      1 2            47     51
 3     1      1 2            39     41
 4    47      1 2            33     41
 5    65      1 1            42     56
 6     4      1 2            39     51
 7   131      1 3            46     66
 8   106      1 1            42     41
 9    37      1 2            39     51
10    73      1 1            39     56
# … with 31 more rows
iris %>% filter(Species=="setosa", Sepal.Width>4)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.7         4.4          1.5         0.4  setosa
2          5.2         4.1          1.5         0.1  setosa
3          5.5         4.2          1.4         0.2  setosa
iris %>% filter(Species=="versicolor", Petal.Length<4)
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1           4.9         2.4          3.3         1.0 versicolor
2           5.2         2.7          3.9         1.4 versicolor
3           5.0         2.0          3.5         1.0 versicolor
4           5.6         2.9          3.6         1.3 versicolor
5           5.6         2.5          3.9         1.1 versicolor
6           5.7         2.6          3.5         1.0 versicolor
7           5.5         2.4          3.8         1.1 versicolor
8           5.5         2.4          3.7         1.0 versicolor
9           5.8         2.7          3.9         1.2 versicolor
10          5.0         2.3          3.3         1.0 versicolor
11          5.1         2.5          3.0         1.1 versicolor
# Question: filter iris dataset for Species equal to "setosa" or "virginica"

arrange: reorder cases

icecream %>% arrange(video) # order 'video' column in ascending order
# A tibble: 200 x 5
      id female ice_cream video puzzle
   <int>  <int> <fct>     <int>  <int>
 1    15      0 3            26     42
 2    45      1 2            29     26
 3    38      0 2            31     56
 4    51      1 3            31     39
 5    67      0 2            33     32
 6    47      1 2            33     41
 7   134      0 2            34     46
 8   133      0 1            34     31
 9    44      1 2            34     46
10    46      1 2            34     41
# … with 190 more rows
icecream %>% arrange(desc(puzzle)) # order 'puzzle' column in descending order
# A tibble: 200 x 5
      id female ice_cream video puzzle
   <int>  <int> <fct>     <int>  <int>
 1    95      0 3            61     71
 2   192      0 3            66     71
 3   183      0 1            55     71
 4   100      1 3            69     71
 5   180      1 3            58     71
 6   139      1 1            55     71
 7    59      1 1            55     71
 8    23      1 2            58     71
 9   143      0 1            72     66
10   154      0 3            61     66
# … with 190 more rows
as_tibble(iris) %>% arrange(Petal.Length)
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          4.6         3.6          1           0.2 setosa 
 2          4.3         3            1.1         0.1 setosa 
 3          5.8         4            1.2         0.2 setosa 
 4          5           3.2          1.2         0.2 setosa 
 5          4.7         3.2          1.3         0.2 setosa 
 6          5.4         3.9          1.3         0.4 setosa 
 7          5.5         3.5          1.3         0.2 setosa 
 8          4.4         3            1.3         0.2 setosa 
 9          5           3.5          1.3         0.3 setosa 
10          4.5         2.3          1.3         0.3 setosa 
# … with 140 more rows
as_tibble(iris) %>% arrange(desc(Sepal.Length))
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
          <dbl>       <dbl>        <dbl>       <dbl> <fct>    
 1          7.9         3.8          6.4         2   virginica
 2          7.7         3.8          6.7         2.2 virginica
 3          7.7         2.6          6.9         2.3 virginica
 4          7.7         2.8          6.7         2   virginica
 5          7.7         3            6.1         2.3 virginica
 6          7.6         3            6.6         2.1 virginica
 7          7.4         2.8          6.1         1.9 virginica
 8          7.3         2.9          6.3         1.8 virginica
 9          7.2         3.6          6.1         2.5 virginica
10          7.2         3.2          6           1.8 virginica
# … with 140 more rows
# Question: 1) filter iris dataset for Species equal to "setosa" and 2) sort  in descending order of Sepal.Width  

mutate: add new variables that are functions of existing variables

icecream_new <- icecream %>% mutate(puzzle100 = puzzle*100)
icecream_new
# A tibble: 200 x 6
      id female ice_cream video puzzle puzzle100
   <int>  <int> <fct>     <int>  <int>     <dbl>
 1    70      0 2            47     57      5700
 2   121      1 1            63     61      6100
 3    86      0 3            58     31      3100
 4   141      0 3            53     56      5600
 5   172      0 1            53     61      6100
 6   113      0 1            63     61      6100
 7    50      0 1            53     61      6100
 8    11      0 1            39     36      3600
 9    84      0 1            58     51      5100
10    48      0 1            50     51      5100
# … with 190 more rows
# Question: 1) filter icecream dataset for ice_cream equal to 1, 2) create video1000 (video*1000) column, 3) sort in descending order of video1000, 4) assign the dataset to icecream_new2

summarise: condense multiple values to a single value

icecream %>% summarise(Mean_video=mean(video), SD_video=sd(video), SD_median=median(video))
# A tibble: 1 x 3
  Mean_video SD_video SD_median
       <dbl>    <dbl>     <dbl>
1       51.8     9.90        53

group_by: break down a dataset into specified groups of rows

puzzle.summary <- icecream %>% group_by(ice_cream) %>% summarise(Mean=mean(puzzle),
            Variance=var(puzzle))%>%as.data.frame()

iris %>% group_by(Species) %>% summarise(Mean=mean(Sepal.Length), Median=median(Sepal.Length), Variance=var(Sepal.Length))
# A tibble: 3 x 4
  Species     Mean Median Variance
  <fct>      <dbl>  <dbl>    <dbl>
1 setosa      5.01    5      0.124
2 versicolor  5.94    5.9    0.266
3 virginica   6.59    6.5    0.404
# Question: 1) group by Species 2) calculate mean, median, var, min, max of Sepal.Length for each group 3) sort data in descending order of mean 3) convert to a data frame  4) assign the output to "iris_new"

Average and standard deviation by icecream flavor

icecream %>% group_by(ice_cream) %>% 
  summarise(Mean=mean(puzzle),Variance=var(puzzle))
# A tibble: 3 x 3
  ice_cream  Mean Variance
  <fct>     <dbl>    <dbl>
1 1          52.0     99.5
2 2          47.3    117. 
3 3          57.1     99.2
puzzle.summary <-icecream %>% group_by(ice_cream) %>% 
  summarise(Mean=mean(puzzle), Variance=var(puzzle) )
puzzle.summary
# A tibble: 3 x 3
  ice_cream  Mean Variance
  <fct>     <dbl>    <dbl>
1 1          52.0     99.5
2 2          47.3    117. 
3 3          57.1     99.2
class(puzzle.summary)
[1] "tbl_df"     "tbl"        "data.frame"
puzzle.summary <- icecream %>% group_by(ice_cream) %>% 
  summarise(Mean=mean(puzzle),
            Variance=var(puzzle) )%>%as.data.frame()
puzzle.summary
  ice_cream     Mean  Variance
1         1 52.03158  99.45644
2         2 47.31915 117.43941
3         3 57.13793  99.24380
class(puzzle.summary)
[1] "data.frame"

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.8.3     workflowr_1.4.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2       knitr_1.24       whisker_0.3-2    magrittr_1.5    
 [5] tidyselect_0.2.5 R6_2.4.0         rlang_0.4.0      fansi_0.4.0     
 [9] stringr_1.4.0    tools_3.6.1      xfun_0.9         utf8_1.1.4      
[13] cli_1.1.0        git2r_0.26.1     htmltools_0.3.6  yaml_2.2.0      
[17] rprojroot_1.3-2  digest_0.6.20    assertthat_0.2.1 tibble_2.1.3    
[21] crayon_1.3.4     purrr_0.3.2      vctrs_0.2.0      fs_1.3.1        
[25] zeallot_0.1.0    glue_1.3.1       evaluate_0.14    rmarkdown_1.15  
[29] stringi_1.4.3    compiler_3.6.1   pillar_1.4.2     backports_1.1.4 
[33] pkgconfig_2.0.2