read.xls() wraps around read.table()
Remember how read.xls() actually works? It basically comes down to two steps: converting the Excel file to a .csv file using a Perl script, and then reading that .csv file with the read.csv() function that is loaded by default in R, through the utils package.
This means that all the options that you can specify in read.csv(), can also be specified in read.xls().
The urbanpop.xls dataset is already available in your workspace. It's still comprised of three sheets, and has column names in the first row of each sheet.
This exercise is part of the course
Importing Data in R (Part 1)
Exercise instructions
- Finish the
read.xls()call that reads data from the second sheet ofurbanpop.xls: skip the first 50 rows of the sheet. Make sure to setheaderappropriately and that the country names are not imported as factors. - Print the first 10 observations of
urban_popwithhead().
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The gdata package is alreaded loaded
# Column names for urban_pop
columns <- c("country", paste0("year_", 1967:1974))
# Finish the read.xls call
urban_pop <- read.xls("urbanpop.xls", sheet = 2,
skip = ___, header = ___, stringsAsFactors = ___,
col.names = columns)
# Print first 10 observation of urban_pop