Some R packages comes with datasets. The package needs to be installed before being used.
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 dis rad tax ptratio black lstat medv
## 1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
## 2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
## 3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
## 4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 33.4
## 5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33 36.2
## 6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 394.12 5.21 28.7
## [1] "crim" "zn" "indus" "chas" "nox" "rm" "age"
## [8] "dis" "rad" "tax" "ptratio" "black" "lstat" "medv"
## [1] 506 14
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
## [1] 5000 1
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.
## Speed
## 1 299.85
## 2 299.74
## 3 299.90
## 4 300.07
## 5 299.93
## 6 299.85
## [1] 100 1
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”.
<!- endofyank->