Prerequisites
- No additional R packages are necessary for this notebook to run
- Cells in this notebook expect the Car Sales.csv file to be in certain locations; specifics are in the cell itself
- Resources to help you practice are available at the end
Load Data From a CSV File
File is in the same directory as your Jupyter Notebook
# Read the CSV file
car.sales <- read.csv("Car Sales.csv")
# Show the first 5 rows
head(car.sales, 5)
DealershipName | RedCars | SilverCars | BlackCars | BlueCars | MonthSold | YearSold |
Clyde's Clunkers | 902 | 650 | 754 | 792 | 1 | 2018 |
Clyde's Clunkers | 710 | 476 | 518 | 492 | 2 | 2018 |
Clyde's Clunkers | 248 | 912 | 606 | 350 | 3 | 2018 |
Clyde's Clunkers | 782 | 912 | 858 | 446 | 4 | 2018 |
Clyde's Clunkers | 278 | 354 | 482 | 752 | 5 | 2018 |
File is in a different directory than your Jupyter Notebook
The example will use your “home directory” to make this example applicable across operating systems, but you can use any path as long as the file exists there…
# Make sure to use "\" slashes and not "/" slashes (it's a Windows thing)
# There actually needs to be folders named "Path" and "To" and "CSV" and "File"
# in your home directory (the "~" means "home directory") for this cell to work
csv.file.path = normalizePath("~\\Path\\To\\CSV\\File\\Car Sales.csv", winslash = "\\")
other.path.car.sales <- read.csv(csv.file.path)
# Show the first 5 rows
head(other.path.car.sales, 5)
DealershipName | RedCars | SilverCars | BlackCars | BlueCars | MonthSold | YearSold |
Clyde's Clunkers | 902 | 650 | 754 | 792 | 1 | 2018 |
Clyde's Clunkers | 710 | 476 | 518 | 492 | 2 | 2018 |
Clyde's Clunkers | 248 | 912 | 606 | 350 | 3 | 2018 |
Clyde's Clunkers | 782 | 912 | 858 | 446 | 4 | 2018 |
Clyde's Clunkers | 278 | 354 | 482 | 752 | 5 | 2018 |
From a URL
# Note the URL Encoding with "%20" for spaces
url.to.csv.file <- "https://github.com/andrewcbancroft/datadaylife-blog/raw/master/datasets/Car%20Sales.csv"
# Read the CSV file
url.car.sales = read.(url_to_csv_file)
# Show the first 5 rows
head(url.car.sales, 5)
Error in read.table(file = file, header = header, sep = sep, quote = quote, : object 'url_to_csv_file' not found
Traceback:
1. read.csv2(url_to_csv_file)
2. read.table(file = file, header = header, sep = sep, quote = quote,
. dec = dec, fill = fill, comment.char = comment.char, ...)