skip and n_max
Through skip
and n_max
you can control which part of your flat file you're actually importing into R.
skip
specifies the number of lines you're ignoring in the flat file before actually starting to import data.n_max
specifies the number of lines you're actually importing.
Say for example you have a CSV file with 20 lines, and set skip = 2
and n_max = 3
, you're only reading in lines 3, 4 and 5 of the file.
Watch out: Once you skip
some lines, you also skip the first line that can contain column names!
potatoes.txt
(view), a flat file with tab-delimited records and without column names, is available in your workspace.
This is a part of the course
“Introduction to Importing Data in R”
Exercise instructions
- Finish the first
read_tsv()
call to import observations 7, 8, 9, 10 and 11 frompotatoes.txt
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Column names
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
# Import 5 observations from potatoes.txt: potatoes_fragment
potatoes_fragment <- read_tsv("potatoes.txt", skip = ___, n_max = ___, col_names = properties)
This exercise is part of the course
Introduction to Importing Data in R
In this course, you will learn to read CSV, XLS, and text files in R using tools like readxl and data.table.
In addition to base R, there are dedicated packages to easily and efficiently import flat file data. We'll talk about two such packages: readr and data.table.
Exercise 1: readr: read_csv & read_tsvExercise 2: read_csvExercise 3: read_tsvExercise 4: readr: read_delimExercise 5: read_delimExercise 6: skip and n_maxExercise 7: col_typesExercise 8: col_types with collectorsExercise 9: data.table: freadExercise 10: freadExercise 11: fread: more advanced useExercise 12: Dedicated classesWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.