1. Load from Packages

Some R packages comes with datasets. The package needs to be installed before being used.

install.packages("MASS")     # you must install packages if not installed yet

Packages only need to be installed once, but it needs to be loaded every time R is restarted. Use library() command to load packages.

library(MASS)     # loads pacckage called MASS
        data(Boston)      # loads the data called Boston which is inside MASS package
        head(Boston)      # see only first few lines of Boston
##      crim zn indus chas   nox    rm  age
## 1 0.00632 18  2.31    0 0.538 6.575 65.2
## 2 0.02731  0  7.07    0 0.469 6.421 78.9
## 3 0.02729  0  7.07    0 0.469 7.185 61.1
## 4 0.03237  0  2.18    0 0.458 6.998 45.8
## 5 0.06905  0  2.18    0 0.458 7.147 54.2
## 6 0.02985  0  2.18    0 0.458 6.430 58.7
##      dis rad tax ptratio  black lstat medv
## 1 4.0900   1 296    15.3 396.90  4.98 24.0
## 2 4.9671   2 242    17.8 396.90  9.14 21.6
## 3 4.9671   2 242    17.8 392.83  4.03 34.7
## 4 6.0622   3 222    18.7 394.63  2.94 33.4
## 5 6.0622   3 222    18.7 396.90  5.33 36.2
## 6 6.0622   3 222    18.7 394.12  5.21 28.7
names(Boston)     # list of column names in Boston
##  [1] "crim"    "zn"      "indus"   "chas"
##  [5] "nox"     "rm"      "age"     "dis"
##  [9] "rad"     "tax"     "ptratio" "black"
## [13] "lstat"   "medv"
dim(Boston)       # check dimention of Boston
## [1] 506  14
Boston            # see all the numbers inside "Boston"
?Boston           # see description of Boston data
plot(Boston$crim, Boston$medv)      # plot crim rate vs median home value



2. Load Directly from Web

Sometimes data is available to be directly loaded from web.

Orig <- read.csv("https://nmimoto.github.io/datasets/pi.csv")
        #- Load the dataset directly from my website.
head(Orig)
##   digits
## 1      3
## 2      1
## 3      4
## 4      1
## 5      5
## 6      9
dim(Orig)
## [1] 5000    1
hist(Orig$digits)     # histogram of 'digits' column



3. Load from Local Drive

You can also load a csv file that is stored in your local hard drive. Make sure the file is in your current working working directly.

Say you went to “https://nmimoto.github.io/datasets/” and downladed “light.csv” to your current working directly. Then you can do the following script load it into R.

Orig <- read.csv("light.csv")  # loads the data from your working directory.
head(Orig)
##    Speed
## 1 299.85
## 2 299.74
## 3 299.90
## 4 300.07
## 5 299.93
## 6 299.85
dim(Orig)
## [1] 100   1
hist(Orig$Speed)         # scatter plot of 'Speed' column

Alternatively, if you want to keep the dataset in a different folder, but still want to load it without changing the working directory, you can specify the entire file path insetead of just the file name “light.csv”.

Orig <- read.csv("C://User/nmimoto/Documents/data/light.csv", header=TRUE)  # loads the data from your working directory.



Code Only

### 1. Load from Packages
# Do this if not installed already (once per PC)
# install.packages("MASS")     # you must install packages if not installed yet

library(MASS)     # loads pacckage called MASS
data(Boston)      # loads the data called Boston which is inside MASS package
head(Boston)      # see only first few lines of Boston
names(Boston)     # list of column names in Boston
dim(Boston)       # check dimention of Boston


Boston            # see all the numbers inside "Boston"
?Boston           # see description of Boston data


plot(Boston$crim, Boston$medv)      # plot crim rate vs median home value


### 2. Load Directly from Web
Orig <- read.csv("https://nmimoto.github.io/datasets/pi.csv")
        #- Load the dataset directly from my website.
head(Orig)
dim(Orig)
hist(Orig$digits)     # histogram of 'digits' column


### 3. Load from Local Drive
Orig <- read.csv("light.csv")  # loads the data from your working directory.
head(Orig)
dim(Orig)
hist(Orig$Speed)         # scatter plot of 'Speed' column


# Alternatively
Orig <- read.csv("C://User/nmimoto/Documents/data/light.csv", header=TRUE)  # loads the data from your working directory.