问题
I am working through this book dealing with statistical learning/machine learning and R. One of the problem states:
To begin, load in the Boston data set. The Boston data set is part of the MASS library in R.
library (MASS)
Now the data set is contained in the object
Boston.Read about the data set:
?Boston
I don't understand the syntax library(MASS). How do I get the Boston data set from this? I've tried Boston=library(MASS) but that gives me an array of words:
"MASS" "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"
I've also tried something like Boston=library(MASS::Boston) but that doesn't seem to be valid either.
回答1:
You don't have to assign the data to a new object also called Boston, data() loads the object Boston into the global environment:
> library(MASS)
> data(Boston)
> head(Boston)
crim zn indus chas nox rm age dis rad tax ptratio black lstat
1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98
2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14
3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03
4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94
5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33
6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 394.12 5.21
medv
1 24.0
2 21.6
3 34.7
4 33.4
5 36.2
6 28.7
>
回答2:
The library function will loads a package in your workspace, in this case of:
library (MASS)
this will be the MASS package. This package already contains the Boston data-frame which can be accessed immediately.
来源:https://stackoverflow.com/questions/24872467/how-do-i-load-data-set-part-of-the-mass-library-in-r