The skip argument
Another argument that can be very useful when reading in Excel files that are less tidy, is skip
. With skip
, you can tell R to ignore a specified number of rows inside the Excel sheets you're trying to pull data from. Have a look at this example:
read_excel("data.xlsx", skip = 15)
In this case, the first 15 rows in the first sheet of "data.xlsx"
are ignored.
If the first row of this sheet contained the column names, this information will also be ignored by readxl
. Make sure to set col_names
to FALSE
or manually specify column names in this case!
The file urbanpop.xlsx
(view) is available in your directory; it has column names in the first rows.
This is a part of the course
“Introduction to Importing Data in R”
Exercise instructions
- Import the second sheet of
"urbanpop.xlsx"
, but skip the first 21 rows. Make sure to setcol_names = FALSE
. Store the resulting data frame in a variableurbanpop_sel
. - Select the first observation from
urbanpop_sel
and print it out.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the second sheet of urbanpop.xlsx, skipping the first 21 rows: urbanpop_sel
urbanpop_sel <- read_excel("urbanpop.xlsx", sheet = ___, col_names =___, skip = ___)
# Print out the first observation from urbanpop_sel
___
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.
Excel is a widely used data analysis tool. If you prefer to do your analyses in R, though, you'll need an understanding of how to import .csv data into R. This chapter will show you how to use readxl to do so.
Exercise 1: readxl (1)Exercise 2: List the sheets of an Excel fileExercise 3: Import an Excel sheetExercise 4: Reading a workbookExercise 5: readxl (2)Exercise 6: The col_names argumentExercise 7: The skip argumentWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.