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
1# Read the CSV file
2car.sales <- read.csv("Car Sales.csv")
3
4# Show the first 5 rows
5head(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…
1# Make sure to use "\" slashes and not "/" slashes (it's a Windows thing)
2# There actually needs to be folders named "Path" and "To" and "CSV" and "File"
3# in your home directory (the "~" means "home directory") for this cell to work
4csv.file.path = normalizePath("~\\Path\\To\\CSV\\File\\Car Sales.csv", winslash = "\\")
5
6other.path.car.sales <- read.csv(csv.file.path)
7
8# Show the first 5 rows
9head(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
1# Note the URL Encoding with "%20" for spaces
2url.to.csv.file <- "/datasets/Car%20Sales.csv"
3
4# Read the CSV file
5url.car.sales = read.(url_to_csv_file)
6
7# Show the first 5 rows
8head(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, ...)