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”
Exercise instructions
- Import the first Excel sheet of
"urbanpop_nonames.xlsx"
and store the result inpop_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 thecols
vector that has already been prepared for you to specify the column names. Store the resulting data frame inpop_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
___
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.