Practice: read_csv

These exercises check your understanding of file loading and some useful arguments for skipping or reading a certain number of rows. Keep these arguments in mind, as they come in handy when files have multi-line titles that are not part of the data. Complete these exercises to practice CSV loading. Also, try to load some files from your computer.

Exercises

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

  2. Apart from file, skip, and comment, what other arguments do read_csv() and read_tsv() have in common?

  3. What are the most important arguments to read_fwf()?

  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'"
  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")
 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")


Source: H. Wickham and G. Grolemund, https://r4ds.had.co.nz/data-import.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.

Last modified: Monday, January 9, 2023, 3:49 PM