close
Reading data from files
read.table() 或是 scan()
利用read.table讀取houses.txt
1: > HousePrice <- read.table("C:\\statistics R\\houses.txt")
2: > HousePrice
3: Price Floor Area Rooms Age Cent.heat
4: 01 52.00 111 830 5 6.2 no
5: 02 54.75 128 710 5 7.5 no
6: 03 57.50 101 1000 5 4.2 no
7: 04 57.50 131 690 6 8.8 no
8: 05 59.75 93 900 5 1.9 yes
取出Price行資料
1: > HousePrice[1]
2: Price
3: 01 52.00
4: 02 54.75
5: 03 57.50
6: 04 57.50
7: 05 59.75
取出Floor行資料
1: > HousePrice[2]
2: Floor
3: 01 111
4: 02 128
5: 03 101
6: 04 131
7: 05 93
如果要取出其中一筆資料呢? 例如HousePrice第一行中的第一筆和第二筆資料:
1: > HousePrice[[1]][1]
2: [1] 52
3: > HousePrice[[1]][2]
4: [1] 54.75
The scan() function
假設第1行為character 第2,3行為numeric資料
利用scan()讀取input.txt
1: > inp <- scan("C:\\statistics R\\input.txt", list("",0,0))
2: Read 5 records
3: > inp
4: [[1]]
5: [1] "Jane" "Peter" "John" "Ryan" "Irene"
6:
7: [[2]]
8: [1] 52.00 54.75 57.50 57.50 59.75
9:
10: [[3]]
11: [1] 111 128 101 131 93
假設John的第一筆資料無法取得, 請填入NA
利用scan()讀取input2.txt
讀入的資料存成三個變數分別為label, x, y
1: > label <- inp[[1]]; x <- inp[[2]]; y <- inp[[3]]
2: > label
3: [1] "Jane" "Peter" "John" "Ryan" "Irene"
4: > x
5: [1] 52.00 54.75 NA 57.50 59.75
6: > y
7: [1] 111 128 101 131 93
另外, 可以在scan下參數指定回傳的變數名稱, 分別為 name, x1, x2
1: > inp <- scan("C:\\statistics R\\input2.txt", list(name="",x1=0,x2=0))
2: Read 5 records
取出來的方式有三種:
方法1: variable_name$fieldname
1: > inp$name
2: [1] "Jane" "Peter" "John" "Ryan" "Irene"
3: > inp$x1
4: [1] 52.00 54.75 NA 57.50 59.75
5: > inp$x2
方法2 variable_name[“fieldname”]
1: > inp["name"]
2: $name
3: [1] "Jane" "Peter" "John" "Ryan" "Irene"
4:
5: > inp["x1"]
6: $x1
7: [1] 52.00 54.75 NA 57.50 59.75
8:
9: > inp["x2"]
10: $x2
11: [1] 111 128 101 131 93
方法3: variable_name[ index ]
1: > inp[[1]]
2: [1] "Jane" "Peter" "John" "Ryan" "Irene"
3: > inp[[2]]
4: [1] 52.00 54.75 NA 57.50 59.75
5: > inp[[3]]
6: [1] 111 128 101 131 93
-----------------------------------------------------------------------------------------------------------------------------
data()可以列出R提供內建的資料庫
假設欲載入infert資料可以下指令data(infert), 接著下ls()指令觀察目前workspace所有變數名稱
1: > data()
2: > data(infert)
3: > ls()
4: [1] "HousePrice" "infert" "inp" "label" "x" "y"
編輯edit()
1: > edit(x)
1: > edit(inp)
全站熱搜