如何在R语言的基础包或其他包中获取可用数据集的列表?


R语言的基础包和不同的R包中有很多数据集可用。这些数据集的特征差别很大,例如,有些数据集是时间序列数据,有些只有数值列,有些既有数值列也有因子列,有些包含字符列和其他类型的列。因此,对于想要学习R编程的人来说,了解这些数据集非常有用。要获取R基础包中可用数据集的列表,可以使用`data()`函数;要获取某个包中可用数据集的列表,则需要先加载该包,然后`data()`命令会显示该包中可用的数据集。此外,对于R基础包中的数据集,还可以使用`ls("package:datasets")`。

示例

 在线演示

> data()

输出

Data sets in package ‘datasets’:
AirPassengers    Monthly Airline Passenger Numbers 1949-1960
BJsales          Sales Data with Leading Indicator
BJsales.lead (BJsales)
                  Sales Data with Leading Indicator
BOD                Biochemical Oxygen Demand
CO2                Carbon Dioxide Uptake in Grass Plants
ChickWeight       Weight versus age of chicks on different diets
DNase             Elisa assay of DNase
EuStockMarkets    Daily Closing Prices of Major European Stock
                  Indices, 1991-1998
Formaldehyde       Determination of Formaldehyde
HairEyeColor       Hair and Eye Color of Statistics Students
Harman23.cor       Harman Example 2.3
Harman74.cor       Harman Example 7.4
Indometh          Pharmacokinetics of Indomethacin
InsectSprays       Effectiveness of Insect Sprays
JohnsonJohnson       Quarterly Earnings per Johnson & Johnson Share
LakeHuron          Level of Lake Huron 1875-1972
LifeCycleSavings    Intercountry Life-Cycle Savings Data
Loblolly             Growth of Loblolly pine trees
Nile                Flow of the River Nile
Orange             Growth of Orange Trees
OrchardSprays       Potency of Orchard Sprays
PlantGrowth       Results from an Experiment on Plant Growth
Puromycin          Reaction Velocity of an Enzymatic Reaction
Seatbelts          Road Casualties in Great Britain 1969-84
Theoph             Pharmacokinetics of Theophylline
Titanic          Survival of passengers on the Titanic
ToothGrowth       The Effect of Vitamin C on Tooth Growth in
                  Guinea Pigs
UCBAdmissions    Student Admissions at UC Berkeley
UKDriverDeaths    Road Casualties in Great Britain 1969-84
UKgas             UK Quarterly Gas Consumption
USAccDeaths       Accidental Deaths in the US 1973-1978
USArrests          Violent Crime Rates by US State
USJudgeRatings    Lawyers' Ratings of State Judges in the US
                  Superior Court
USPersonalExpenditure    Personal Expenditure Data
UScitiesD          Distances Between European Cities and Between
                  US Cities
VADeaths          Death Rates in Virginia (1940)
WWWusage          Internet Usage per Minute
WorldPhones       The World's Telephones
ability.cov       Ability and Intelligence Tests
airmiles          Passenger Miles on Commercial US Airlines,
                     1937-1960
airquality       New York Air Quality Measurements
anscombe          Anscombe's Quartet of 'Identical' Simple Linear
                  Regressions
attenu          The Joyner-Boore Attenuation Data
attitude          The Chatterjee-Price Attitude Data
austres          Quarterly Time Series of the Number of
               Australian Residents
beaver1          (beavers) Body Temperature Series of Two Beavers
beaver2          (beavers) Body Temperature Series of Two Beavers
cars          Speed and Stopping Distances of Cars
chickwts       Chicken Weights by Feed Type
co2          Mauna Loa Atmospheric CO2 Concentration
crimtab       Student's 3000 Criminals Data
discoveries    Yearly Numbers of Important Discoveries
esoph          Smoking, Alcohol and (O)esophageal Cancer
euro          Conversion Rates of Euro Currencies
euro.cross (euro)    Conversion Rates of Euro Currencies
eurodist       Distances Between European Cities and Between
               US Cities
faithful       Old Faithful Geyser Data
fdeaths (UKLungDeaths)
               Monthly Deaths from Lung Diseases in the UK
freeny          Freeny's Revenue Data
freeny.x (freeny)    Freeny's Revenue Data
freeny.y (freeny)    Freeny's Revenue Data
infert          Infertility after Spontaneous and Induced
                  Abortion
iris          Edgar Anderson's Iris Data
iris3          Edgar Anderson's Iris Data
islands          Areas of the World's Major Landmasses
ldeaths (UKLungDeaths)
               Monthly Deaths from Lung Diseases in the UK
lh             Luteinizing Hormone in Blood Samples
longley       Longley's Economic Regression Data
lynx          Annual Canadian Lynx trappings 1821-1934
mdeaths (UKLungDeaths)
               Monthly Deaths from Lung Diseases in the UK
morley          Michelson Speed of Light Data
mtcars          Motor Trend Car Road Tests
nhtemp          Average Yearly Temperatures in New Haven
nottem          Average Monthly Temperatures at Nottingham,
               1920-1939
npk             Classical N, P, K Factorial Experiment
occupationalStatus    Occupational Status of Fathers and their Sons
precip          Annual Precipitation in US Cities
presidents       Quarterly Approval Ratings of US Presidents
pressure          Vapor Pressure of Mercury as a Function of
               Temperature
quakes             Locations of Earthquakes off Fiji
randu             Random Numbers from Congruential Generator
               RANDU
rivers          Lengths of Major North American Rivers
rock             Measurements on Petroleum Rock Samples
sleep             Student's Sleep Data
stack.loss (stackloss)
                  Brownlee's Stack Loss Plant Data
stack.x (stackloss)    Brownlee's Stack Loss Plant Data
stackloss          Brownlee's Stack Loss Plant Data
state.abb (state)    US State Facts and Figures
state.area (state)    US State Facts and Figures
state.center (state)    US State Facts and Figures
state.division (state)
                     US State Facts and Figures
state.name (state)    US State Facts and Figures
state.region (state)    US State Facts and Figures
state.x77 (state)    US State Facts and Figures
sunspot.month          Monthly Sunspot Data, from 1749 to "Present"
sunspot.year          Yearly Sunspot Data, 1700-1988
sunspots             Monthly Sunspot Numbers, 1749-1983
swiss                Swiss Fertility and Socioeconomic Indicators
                  (1888) Data
treering          Yearly Treering Data, -6000-1979
trees          Diameter, Height and Volume for Black Cherry
               Trees
uspop          Populations Recorded by the US Census
volcano          Topographic Information on Auckland's Maunga
               Whau Volcano
warpbreaks       The Number of Breaks in Yarn during Weaving
women          Average Heights and Weights for American Women

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

 在线演示

> ls("package:datasets")

输出

[1] "ability.cov"       "airmiles"    "AirPassengers"
[4] "airquality"       "anscombe"       "attenu"
[7] "attitude"          "austres"       "beaver1"
[10] "beaver2"       "BJsales"       "BJsales.lead"
[13] "BOD"             "cars"          "ChickWeight"
[16] "chickwts"          "co2"          "CO2"
[19] "crimtab"       "discoveries"       "DNase"
[22] "esoph"          "euro"          "euro.cross"
[25] "eurodist"    "EuStockMarkets"    "faithful"
[28] "fdeaths"    "Formaldehyde"       "freeny"
[31] "freeny.x"    "freeny.y"          "HairEyeColor"
[34] "Harman23.cor" "Harman74.cor"    "Indometh"
[37] "infert"       "InsectSprays"       "iris"
[40] "iris3"       "islands"       "JohnsonJohnson"
[43] "LakeHuron"    "ldeaths"          "lh"
[46] "LifeCycleSavings" "Loblolly"    "longley"
[49] "lynx"          "mdeaths"       "morley"
[52] "mtcars"       "nhtemp"          "Nile"
[55] "nottem"       "npk"       "occupationalStatus"
[58] "Orange"    "OrchardSprays"    "PlantGrowth"
[61] "precip"    "presidents"       "pressure"
[64] "Puromycin"    "quakes"       "randu"
[67] "rivers"          "rock"       "Seatbelts"
[70] "sleep"       "stack.loss"       "stack.x"
[73] "stackloss"    "state.abb"       "state.area"
[76] "state.center" "state.division" "state.name"
[79] "state.region" "state.x77"       "sunspot.month"
[82] "sunspot.year"    "sunspots"        "swiss"
[85] "Theoph"       "Titanic"       "ToothGrowth"
[88] "treering"       "trees"       "UCBAdmissions"
[91] "UKDriverDeaths" "UKgas"       "USAccDeaths"
[94] "USArrests"    "UScitiesD"       "USJudgeRatings"
[97] "USPersonalExpenditure" "uspop"    "VADeaths"
[100] "volcano"       "warpbreaks"       "women"
[103] "WorldPhones"    "WWWusage"

要查看某个包中的数据集,需要加载该包,然后使用`data()`函数。这样,在显示所有R基础包数据集的窗口底部,就能找到该包中可用的数据集列表。

示例

> library(ggplot2)
> data()

输出

Data sets in package ‘ggplot2’:
diamonds       Prices of 50,000 round cut diamonds
economics       US economic time series
economics_long    US economic time series
faithfuld       2d density estimate of Old Faithful data
luv_colours    'colors()' in Luv space
midwest       Midwest demographics
mpg          Fuel economy data from 1999 and 2008 for 38
               popular models of car
msleep          An updated and expanded version of the mammals
               sleep dataset
presidential    Terms of 11 presidents from Eisenhower to Obama
seals          Vector field of seal movements
txhousing       Housing sales in TX

现在可以使用ggplot2包中的任何数据集,例如查看msleep数据的前面几行:

示例

> head(msleep)
# A tibble: 6 x 11

输出

name genus vore order conservation sleep_total sleep_rem sleep_cycle awake
<chr> <chr> <chr> <chr> <chr>       <dbl>    <dbl> <dbl> <dbl>
1 Chee~ Acin~ carni Carn~ lc          12.1    NA    NA    11.9
2 Owl ~ Aotus omni Prim~ <NA>          17    1.8    NA    7
3 Moun~ Aplo~ herbi Rode~ nt          14.4    2.4    NA    9.6
4 Grea~ Blar~ omni Sori~ lc          14.9    2.3    0.133    9.1
5 Cow Bos herbi Arti~ domesticated    4       0.7    0.667    20
6 Thre~ Brad~ herbi Pilo~ <NA>       14.4    2.2    0.767    9.6
# ... with 2 more variables: brainwt <dbl>, bodywt <dbl>

更新于:2020年9月4日

7000+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告