R语言怎么获取内置数据集

您所在的位置:网站首页 r中数据集的调用 R语言怎么获取内置数据集

R语言怎么获取内置数据集

2023-09-10 11:29| 来源: 网络整理| 查看: 265

许多 R 包中含有数据集,可以通过data函数查看或加载这些数据集,通过?获得数据集的帮助文档。

基本数据集

基本包 datasets含有 100 多个数据集(R version 4.2.0),涉及医学、自然、社会学等各个领域。

查看一下:

library(knitr) library(tidyverse) pkg = data(package = "datasets") as_tibble(pkg$results[, c('Item', 'Title')]) ## # A tibble: 104 × 2 ## Item Title ## ## 1 AirPassengers Monthly Airline Passenger Numbers 1949-1960 ## 2 BJsales Sales Data with Leading Indicator ## 3 BJsales.lead (BJsales) Sales Data with Leading Indicator ## 4 BOD Biochemical Oxygen Demand ## 5 CO2 Carbon Dioxide Uptake in Grass Plants ## 6 ChickWeight Weight versus age of chicks on different diets ## 7 DNase Elisa assay of DNase ## 8 EuStockMarkets Daily Closing Prices of Major European Stock Indices,… ## 9 Formaldehyde Determination of Formaldehyde ## 10 HairEyeColor Hair and Eye Color of Statistics Students ## # … with 94 more rows

datasets中的数据集在 R 启动时已经自动加载到环境中了,可以直接使用,例如:

head(iris) ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa

查看iris的帮助文档:

?iris 其他包的数据集

使用其他包的数据集,需要先加载包,再加载数据集。这里以ggplot2包举例:

library(ggplot2) data(diamonds) head(diamonds) ## # A tibble: 6 × 10 ## carat cut color clarity depth table price x y z ## ## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 ## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 ## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 ## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 ## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 ## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48

实际上,当library(ggplot2)执行后,其中的数据集如diamonds,mpg等已经可用了,无需显式加载。

但是有些包library后数据集还不可用,则需要用data函数显式加载。

最后总结data(package = "package_name"),查看 R 包里有哪些数据集data(dataset_name),加载数据集?dataset_name,查看数据集的帮助文档


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3