Data Input 匯入資料

  • 從 R 中載入並執行指令稿中的程式碼
source("script.R")
  • R 在執行程式時,可以順便輸出執行的內容
source("script.R", echo = TRUE)
  • 載入檔案中的歷史指令
    loadhistory("my_history.R")
    
  • R 內建資料

    • R 的 datasets 套件提供了大約 100 個內建的資料集
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
....

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
  • to list the data sets in all available packages.
data(package = .packages(all.available = TRUE))

Data sets in package ‘blotter’:

IBM                      IBM stock price data for one month
amzn                     Hypothetical Intra-Day AMZN Trades
amzn.trades (amzn)       Hypothetical Intra-Day AMZN Trades


Data sets in package ‘survival’:

aml (leukemia)           Acute Myelogenous Leukemia survival data
bladder                  Bladder Cancer Recurrences
bladder1 (bladder)       Bladder Cancer Recurrences
bladder2 (bladder)       Bladder Cancer Recurrences
cancer                   NCCTG Lung Cancer Data
..
  • CSV 檔案輸入

    read.csv("data-1.csv")
    
    • sep 參數可指定分隔字元:
      read.csv("data-tab-1.csv", sep = "\t")
      
    • read.csv 亦可接受 URL:
      read.csv("http://www.your.host/data.csv")
      
  • 文字檔案輸入

    • scan() 可以將檔案中的資料直接讀取進來,變成一個向量:
      scan("data-2.txt")
      
    • 使用 scan 配合樣板讀取資料:
scan("data-3.txt", list(name = "", val.a = 0, val.b = 0))
  • 假設文字檔 data-3.txt 每一筆資料包含一個字串與兩個數值:

John 2.4 33 Joe 3.2 12 Mary 9.2 11

Dan 6 23 Willy 3.9 33

scan("data-3.txt", list(name = "", val.a = 0, val.b = 0))

Read 5 records
$name
[1] "John"  "Joe"   "Mary"  "Dan"   "Willy"

$val.a
[1] 2.4 3.2 9.2 6.0 3.9

$val.b
[1] 33 12 11 23 33
  • 假設文字檔 data-4.txt 都是一些字元,例如:

Mary Joe John
Willy Dan
則使用:

scan("data-4.txt", "")

Read 5 items
[1] "Mary"  "Joe"   "John"  "Willy" "Dan"
  • load 函數則可將儲存的變數載入:
    ``` rm(list = ls()) load("xy.RData")

ls() [1] "x" "y"

ls.str() x : num [1:3] 1.2 3.4 5.6 y : num [1:3] 1.44 11.56 31.36




  * ##### readRDS(): 從 rds 檔案中載入變數,可以使用 readRDS 函數:

women2 <- readRDS("women.rds") identical(women, women2)

```


Reference:

results matching ""

    No results matching ""