Get Started

The col_names argument

Apart from path and sheet, there are several other arguments you can specify in read_excel(). One of these arguments is called col_names.

By default it is TRUE, denoting whether the first row in the Excel sheets contains the column names. If this is not the case, you can set col_names to FALSE. In this case, R will choose column names for you. You can also choose to set col_names to a character vector with names for each column. It works exactly the same as in the readr package.

You'll be working with the urbanpop_nonames.xlsx (view) file. It contains the same data as urbanpop.xlsx (view) but has no column names in the first row of the excel sheets.

This is a part of the course

“Introduction to Importing Data in R”

View Course

Exercise instructions

  • Import the first Excel sheet of "urbanpop_nonames.xlsx" and store the result in pop_a. Have R set the column names of the resulting data frame itself.
  • Import the first Excel sheet of urbanpop_nonames.xlsx; this time, use the cols vector that has already been prepared for you to specify the column names. Store the resulting data frame in pop_b.
  • Print out the summary of pop_a.
  • Print out the summary of pop_b. Can you spot the difference with the other summary?

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import the first Excel sheet of urbanpop_nonames.xlsx (R gives names): pop_a
pop_a <- read_excel("____", col_names = ____)

# Import the first Excel sheet of urbanpop_nonames.xlsx (specify col_names): pop_b
cols <- c("country", paste0("year_", 1960:1966))
pop_b <- ___

# Print the summary of pop_a
___

# Print the summary of pop_b
___
Edit and Run Code

This exercise is part of the course

Introduction to Importing Data in R

BeginnerSkill Level
4.2+
26 reviews

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 argument
Exercise 7: The skip argument

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free