Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with loading data from ISLR package

Tags:

r

I am an R newbie and currently looking at the book An Introduction to Statistical Learning with Applications in R. For many of their examples they use the package ISLR. Unfortunately, I struggle with an example: They install the package (I have tried it in R and RStudio) and execute the following code

Auto=read.table("Auto.data")

When I do that, I get the following error message:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'Auto.data': No such file or directory

I also tried to attach the package with the command library(ISLR) after downloading it - without success. I am not sure if the issue is related to the path of the package but I don't believe so. At least I tried to save the package in my working directory.

I feel a bit stupid as the task looks more than easy. If anyone could help out, it would be much appreciated. Everything related (including a PDF of the book can be found HERE

like image 301
Markus Knopfler Avatar asked Dec 01 '22 13:12

Markus Knopfler


2 Answers

Just use

library(ISLR)

Then the Auto dataset becomes immediately available:

> head(Auto)
  mpg cylinders displacement horsepower weight acceleration year origin                      name
1  18         8          307        130   3504         12.0   70      1 chevrolet chevelle malibu
2  15         8          350        165   3693         11.5   70      1         buick skylark 320
3  18         8          318        150   3436         11.0   70      1        plymouth satellite
4  16         8          304        150   3433         12.0   70      1             amc rebel sst
5  17         8          302        140   3449         10.5   70      1               ford torino
6  15         8          429        198   4341         10.0   70      1          ford galaxie 500

Please read the book carefully. This is an excerpt from page 48:

We begin by loading in the Auto data set. This data is part of the ISLR library (we discuss libraries in Chapter 3) but to illustrate the read.table() function we load it now from a text file. The following command will load the Auto.data file into R and store it as an object called Auto , in a format referred to as a data frame. (The text file data frame can be obtained from this book’s website.)

(Emphasis added).

Here is the link to the file that should be saved in the working directory:

http://www-bcf.usc.edu/~gareth/ISL/Auto.data

Once the file is saved with the name Auto.data in the working directory, the command

Auto <- read.table("Auto.data")

should work without any problem.

Better results will be obtained using

Auto <- read.table("Auto.data", header=TRUE)

as described later in that book.

like image 191
RHertel Avatar answered Dec 15 '22 11:12

RHertel


Install ISLR via install.packages("ISLR") and execute:

library(ISLR)
auto_df = ISLR::Auto

I now have access to the auto dataset, e.g.

auto_df[1,]

    mpg cylinders displacement horsepower weight acceleration ...
1   18  8         307          130        3504   12           ...
like image 20
mwtmurphy Avatar answered Dec 15 '22 10:12

mwtmurphy