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
-
What function would you use to read a file where fields were separated with
"|"? -
Apart from
file
,skip
, andcomment
, what other arguments doread_csv()
andread_tsv()
have in common? -
What are the most important arguments to
read_fwf()
? -
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 toread_csv()
do you need to specify to read the following text into a data frame?"x,y\n1,'a,b'"
-
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 This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.