Last updated: 2019-09-27

Checks: 7 0

Knit directory: wflow-r4ds/

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(20190925) 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:    .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.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


Setup

library(tidyverse)

Exercises

p. 123

How can you tell if an object is a tibble? (Hint: try printing mtcars, which is a regular data frame).

class(mtcars)
[1] "data.frame"
class(as_tibble(mtcars))
[1] "tbl_df"     "tbl"        "data.frame"

Compare and contrast the following operations on a data.frame and equivalent tibble. What is different? Why might the default data frame behaviours cause you frustration?

# data frame
df <- data.frame(abc = 1, xyz = "a")
df$x # Gives you a result (column xyz) even though column x does not exist
[1] a
Levels: a
df[, "xyz"] # returns a vector, string coerced to factor
[1] a
Levels: a
df[, c("abc", "xyz")] # returns a data frame
  abc xyz
1   1   a
# tibble
dft <- tibble(abc = 1, xyz = "a")
dft$x # warns that column x does not exist
Warning: Unknown or uninitialised column: 'x'.
NULL
dft[, "xyz"] # returns tibble, string remains string
# A tibble: 1 x 1
  xyz  
  <chr>
1 a    
dft[, c("abc", "xyz")] # returns tibble
# A tibble: 1 x 2
    abc xyz  
  <dbl> <chr>
1     1 a    

If you have the name of a variable stored in an object, e.g. var <- "mpg", how can you extract the reference variable from a tibble?

var <- "mpg"
head(mtcars[, var])
[1] 21.0 21.0 22.8 21.4 18.7 18.1
head(as_tibble(mtcars)[, var])
# A tibble: 6 x 1
    mpg
  <dbl>
1  21  
2  21  
3  22.8
4  21.4
5  18.7
6  18.1

Practice referring to non-syntactic names in the following data frame by: Extracting the variable called 1. Plotting a scatterplot of 1 vs 2. Creating a new column called 3 which is 2 divided by 1.

Renaming the columns to one, two and three.

annoying <- tibble(
  `1` = 1:10,
  `2` = `1` * 2 + rnorm(length(`1`))
)
annoying$`1`
 [1]  1  2  3  4  5  6  7  8  9 10
annoying[["1"]]
 [1]  1  2  3  4  5  6  7  8  9 10
ggplot(annoying, aes(`1`, `2`)) + geom_point()

annoying <- annoying %>%
  mutate(`3` = `1` + `2`)
annoying %>% rename(one = `1`, two = `2`, three = `3`)
# A tibble: 10 x 3
     one   two three
   <int> <dbl> <dbl>
 1     1  2.20  3.20
 2     2  3.16  5.16
 3     3  5.71  8.71
 4     4  6.51 10.5 
 5     5  8.80 13.8 
 6     6 11.6  17.6 
 7     7 14.8  21.8 
 8     8 15.9  23.9 
 9     9 17.7  26.7 
10    10 19.9  29.9 

What does tibble::enframe() do? When might you use it?

Converts a named vector/list to a 2-column data frame.

What option controls how many additional column names are printed at the footer of a tibble?

tibble.max_extra_cols: Number of extra columns printed in reduced form. Default: 100.


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.2    
[5] readr_1.3.1     tidyr_1.0.0     tibble_2.1.3    ggplot2_3.2.1  
[9] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5  xfun_0.9          haven_2.1.1      
 [4] lattice_0.20-38   colorspace_1.4-1  vctrs_0.2.0      
 [7] generics_0.0.2    htmltools_0.3.6   yaml_2.2.0       
[10] utf8_1.1.4        rlang_0.4.0       pillar_1.4.2     
[13] glue_1.3.1        withr_2.1.2       modelr_0.1.5     
[16] readxl_1.3.1      lifecycle_0.1.0   munsell_0.5.0    
[19] gtable_0.3.0      workflowr_1.4.0   cellranger_1.1.0 
[22] rvest_0.3.4       evaluate_0.14     labeling_0.3     
[25] knitr_1.25        fansi_0.4.0       broom_0.5.2      
[28] Rcpp_1.0.2        scales_1.0.0      backports_1.1.4  
[31] jsonlite_1.6      fs_1.3.1          hms_0.5.1        
[34] digest_0.6.21     stringi_1.4.3     grid_3.6.1       
[37] rprojroot_1.2     cli_1.1.0         tools_3.6.1      
[40] magrittr_1.5      lazyeval_0.2.2    crayon_1.3.4     
[43] pkgconfig_2.0.2   zeallot_0.1.0     xml2_1.2.2       
[46] lubridate_1.7.4   assertthat_0.2.1  rmarkdown_1.15   
[49] httr_1.4.1        rstudioapi_0.10   R6_2.4.0         
[52] nlme_3.1-141      git2r_0.26.1.9000 compiler_3.6.1