R text data input read.table(),scan(), how to use the built-in test data set package:datasets-Alibaba Cloud Developer Community

The name of the data box in the first row. The first column in the second row is the row number. The other columns are data.
Input file form with names and row labels:

     Price    Floor     Area   Rooms     Age  Cent.heat
01   52.00    111.0      830     5       6.2      no
02   54.75    128.0      710     5       7.5      no
03   57.50    101.0     1000     5       4.2      no
04   57.50    131.0      690     6       8.8      no
05   59.75     93.0      900     5       1.9     yes
...

> HousePrice <- read.table("houses.data")
If the row number in the second row is omitted, set the header to TRUE.
Input file form without row labels:

Price    Floor     Area   Rooms     Age  Cent.heat
52.00    111.0      830     5       6.2      no
54.75    128.0      710     5       7.5      no
57.50    101.0     1000     5       4.2      no
57.50    131.0      690     6       8.8      no
59.75     93.0      900     5       1.9     yes
...

HousePrice <- read.table("houses.data", header=TRUE)
scan() reads data from a file and outputs the specified data type.
The second parameter is a dummy type, for example, output to list.
inp <- scan("input.dat", list("",0,0))
label <- inp[[1]]; x <- inp[[2]]; y <- inp[[3]]
Alternatively, you can specify a name for the dummy list.
inp <- scan("input.dat", list(id="", x=0, y=0))
label <- inp$id; x <- inp$x; y <- inp$y
dummy is a numeric type and is output to a vector. A matrix is used to construct a matrix.
X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)
R also has many built-in test data sets, which are included in the package:datasets package.
You can use attach to attach the package to the search path for use.
By default, it may have been bound,
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
use the following methods to view the objects in this package:
> ls(6)
> ls('package:datasets')
> objects(6)
> objects('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"             "USJudgeRatings"        "USPersonalExpenditure"
 [97] "uspop"                 "VADeaths"              "volcano"              
[100] "warpbreaks"            "women"                 "WorldPhones"          
[103] "WWWusage"   
use test data:
for example, uspop is a time series.
> uspop
Time Series:
Start = 1790 
End = 1970 
Frequency = 0.1 
 [1]   3.93   5.31   7.24   9.64  12.90  17.10  23.20  31.40  39.80  50.20
[11]  62.90  76.00  92.00 105.70 122.80 131.70 151.30 179.30 203.20
After you unbind the object, you cannot directly use the object.
> detach('package:datasets')
You can also use the data() function to view the test data objects in the package.
> 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
......
View the test data of all installed packages:
> data(package = .packages(all.available = TRUE))
Data sets in package ‘boot’:

acme                    Monthly Excess Returns
aids                    Delay in AIDS Reporting in England and Wales
aircondit               Failures of Air-conditioning Equipment
aircondit7              Failures of Air-conditioning Equipment
amis                    Car Speeding and Warning Signs
aml                     Remission Times for Acute Myelogenous Leukaemia
......
import test data from other packages:
data(package="rpart")
data(Puromycin, package="datasets")
alternatively, after you use the library to load the package, the test data is automatically loaded to the search path.
[Reference]
1. http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Reading-data-from-files
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now