Last updated: 2020-10-18

Checks: 7 0

Knit directory: r4ds_book/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20200814) 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 results in this page were generated with repository version f81b11a. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

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:    .Rproj.user/

Untracked files:
    Untracked:  VideoDecodeStats/
    Untracked:  analysis/images/
    Untracked:  code_snipp.txt

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 repository in which changes were made to the R Markdown (analysis/ch8_import_data.Rmd) and HTML (docs/ch8_import_data.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd f81b11a sciencificity 2020-10-18 added Chapter 14 and some of Chapter 8

options(scipen=10000)
library(tidyverse)
library(flair)
library(nycflights13)
library(palmerpenguins)
library(gt)
library(skimr)
library(emo)
library(tidyquant)
library(lubridate)
library(magrittr)
theme_set(theme_tq())

Inline csv file

read_csv("a,b,c
1,2,3
4,5,6")
# A tibble: 2 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1     1     2     3
2     4     5     6

Skip some columns

  • metadata
  • commented lines that you don’t want to read
read_csv("The first line of metadata
  The second line of metadata
  x,y,z
  1,2,3", skip = 2)
# A tibble: 1 x 3
      x     y     z
  <dbl> <dbl> <dbl>
1     1     2     3
read_csv("# A comment I want to skip
  x,y,z
  1,2,3", comment = "#")
# A tibble: 1 x 3
      x     y     z
  <dbl> <dbl> <dbl>
1     1     2     3

No column names in data

read_csv("1,2,3\n4,5,6", # \n adds a new line 
         col_names = FALSE) # cols will be labelled seq from X1 .. Xn
# A tibble: 2 x 3
     X1    X2    X3
  <dbl> <dbl> <dbl>
1     1     2     3
2     4     5     6
read_csv("1,2,3\n4,5,6", 
         col_names = c("x", "y", "z")) # cols named as you provided here
# A tibble: 2 x 3
      x     y     z
  <dbl> <dbl> <dbl>
1     1     2     3
2     4     5     6

NA values

read_csv("a,b,c,d\nnull,1,2,.", 
         na = c(".",
                "null"))
# A tibble: 1 x 4
  a         b     c d    
  <lgl> <dbl> <dbl> <lgl>
1 NA        1     2 NA   
# here we specify that the . and null
# must be considered to be missing values

Exercises

  1. What function would you use to read a file where fields were separated with
    “|”?

    read_delim()

    # from the ?read_delim help page
    read_delim("a|b\n1.0|2.0", delim = "|")
    # A tibble: 1 x 2
          a     b
      <dbl> <dbl>
    1     1     2
  2. Apart from file, skip, and comment, what other arguments do read_csv() and read_tsv() have in common?

    All columns are common across the functions.

    • col_names
    • col_types
    • locale
    • na
    • quoted_na
    • quote
    • trim_ws
    • n_max
    • guess_max
    • progress
    • skip_empty_rows
  3. What are the most important arguments to read_fwf()?

    • file to read
    • col_positions as created by fwf_empty(), fwf_widths() or fwf_positions() which tells the function where a column starts and ends.
  4. Sometimes strings in a CSV file contain commas. To prevent them from causing problems they need to be surrounded by a quoting character, like " or '. By default, read_csv() assumes that the quoting character will be ". What argument to read_csv() do you need to specify to read the following text into a data frame?

    "x,y\n1,'a,b'"

    Specify the quote argument.

    read_csv("x,y\n1,'a,b'", quote = "'")
    # A tibble: 1 x 2
          x y    
      <dbl> <chr>
    1     1 a,b  
  5. Identify what is wrong with each of the following inline CSV files. What happens when you run the code?

    read_csv("a,b\n1,2,3\n4,5,6") # only 2 cols specified but 
    read_csv("a,b,c\n1,2\n1,2,3,4")
    read_csv("a,b\n\"1")
    read_csv("a,b\n1,2\na,b")
    read_csv("a;b\n1;3")

    read_csv(“a,b1,2,34,5,6”)
    only 2 cols specified but 3 values provided

    read_csv(“a,b,c1,21,2,3,4”)
    3 col names provided, but either too few, or too many column values provided

    read_csv(“a,b"1”)
    2 col names provided, but only one value provided.
    closing " missing

    read_csv(“a,b1,2,b”) Nothing syntactically a problem, but the rows are filled
    with the column headings?

    read_csv(“a;b1;3”) the read_csv2 which reads ; as delimiters should have been used

    They all run, but most have warnings, and some are not imported as expected.

    read_csv("a,b\n1,2,3\n4,5,6") # only 2 cols specified but 
    Warning: 2 parsing failures.
    row col  expected    actual         file
      1  -- 2 columns 3 columns literal data
      2  -- 2 columns 3 columns literal data
    # A tibble: 2 x 2
          a     b
      <dbl> <dbl>
    1     1     2
    2     4     5
    read_csv("a,b,c\n1,2\n1,2,3,4")
    Warning: 2 parsing failures.
    row col  expected    actual         file
      1  -- 3 columns 2 columns literal data
      2  -- 3 columns 4 columns literal data
    # A tibble: 2 x 3
          a     b     c
      <dbl> <dbl> <dbl>
    1     1     2    NA
    2     1     2     3
    read_csv("a,b\n\"1")
    Warning: 2 parsing failures.
    row col                     expected    actual         file
      1  a  closing quote at end of file           literal data
      1  -- 2 columns                    1 columns literal data
    # A tibble: 1 x 2
          a b    
      <dbl> <chr>
    1     1 <NA> 
    read_csv("a,b\n1,2\na,b")
    # A tibble: 2 x 2
      a     b    
      <chr> <chr>
    1 1     2    
    2 a     b    
    read_csv("a;b\n1;3")
    # A tibble: 1 x 1
      `a;b`
      <chr>
    1 1;3  

sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_South Africa.1252  LC_CTYPE=English_South Africa.1252   
[3] LC_MONETARY=English_South Africa.1252 LC_NUMERIC=C                         
[5] LC_TIME=English_South Africa.1252    

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

other attached packages:
 [1] magrittr_1.5               tidyquant_1.0.0           
 [3] quantmod_0.4.17            TTR_0.23-6                
 [5] PerformanceAnalytics_2.0.4 xts_0.12-0                
 [7] zoo_1.8-7                  lubridate_1.7.8           
 [9] emo_0.0.0.9000             skimr_2.1.1               
[11] gt_0.2.2                   palmerpenguins_0.1.0      
[13] nycflights13_1.0.1         flair_0.0.2               
[15] forcats_0.5.0              stringr_1.4.0             
[17] dplyr_1.0.0                purrr_0.3.4               
[19] readr_1.3.1                tidyr_1.1.0               
[21] tibble_3.0.3               ggplot2_3.3.0             
[23] tidyverse_1.3.0            workflowr_1.6.2           

loaded via a namespace (and not attached):
 [1] httr_1.4.2       jsonlite_1.7.0   modelr_0.1.6     assertthat_0.2.1
 [5] cellranger_1.1.0 yaml_2.2.1       pillar_1.4.6     backports_1.1.6 
 [9] lattice_0.20-38  glue_1.4.1       quadprog_1.5-8   digest_0.6.25   
[13] promises_1.1.0   rvest_0.3.5      colorspace_1.4-1 htmltools_0.5.0 
[17] httpuv_1.5.2     pkgconfig_2.0.3  broom_0.5.6      haven_2.2.0     
[21] scales_1.1.0     whisker_0.4      later_1.0.0      git2r_0.26.1    
[25] generics_0.0.2   ellipsis_0.3.1   withr_2.2.0      repr_1.1.0      
[29] cli_2.0.2        crayon_1.3.4     readxl_1.3.1     evaluate_0.14   
[33] fs_1.4.1         fansi_0.4.1      nlme_3.1-144     xml2_1.3.2      
[37] tools_3.6.3      hms_0.5.3        lifecycle_0.2.0  munsell_0.5.0   
[41] reprex_0.3.0     compiler_3.6.3   rlang_0.4.7      grid_3.6.3      
[45] rstudioapi_0.11  base64enc_0.1-3  rmarkdown_2.4    gtable_0.3.0    
[49] DBI_1.1.0        curl_4.3         R6_2.4.1         knitr_1.28      
[53] utf8_1.1.4       rprojroot_1.3-2  Quandl_2.10.0    stringi_1.4.6   
[57] Rcpp_1.0.4.6     vctrs_0.3.2      dbplyr_1.4.3     tidyselect_1.1.0
[61] xfun_0.13