Download presentation
Presentation is loading. Please wait.
1
A short R session (一個簡短的R課程) 羅琪老師
2
澳大利亞人口資料 讀入一個包含自 年不同時間澳大利亞各州和地區的總人口數等人口指數的檔案到R,然後使用此檔案來建立一個圖表。該檔案的資料如下:
3
變數 Year-a numeric vector NSW-New South Wales population counts
新南威爾士州的人口數量 Vic-Victoria population counts 維多利亞州人口數量 Qld-Queensland population counts 昆士蘭州的人口數量 SA-South Australia population counts 南澳大利亞州的人口數量
4
變數 WA-Western Australia population counts 西澳大利亞州的人口數量
Tas-Tasmania population counts 塔斯馬尼亞州的人口數量 NT-Northern Territory population counts 新界北部地區的人口數量 ACT-Australian Capital Territory population counts 澳大利亞首都地區人口數量 Aust-Population counts for the whole country 澳大利亞全國人口數量
5
將資料檔案austpop.txt讀入R Year NSW Vic. Qld SA WA Tas. NT ACT Aust.
> austpop<-read.table("c:/RData/austpop.txt", header=TRUE) > austpop Year NSW Vic. Qld SA WA Tas. NT ACT Aust. header=TRUE 代表有標題 austpop是一個data frame資料框架
6
畫1917年至1997年間澳大利亞首都特區(ACT)的人口
> plot(ACT ~ Year, data=austpop, pch=16) pch=16 sets the plotting character to a solid black dot 點的樣式為黑色實心點
7
data frame 的變數名稱 R程式中大小寫不同!
> names(austpop) [1] "Year" "NSW" "Vic." "Qld" "SA" "WA" "Tas." "NT" "ACT" "Aust.“ > austpop$year NULL > austpop$Year [1] R程式中大小寫不同!
8
將資料檔案austpop放在工作目錄中 > ACT Error: object 'ACT' not found > austpop$ACT [1] > attach(austpop) 要使用austpop這個data frame中的變數ACT austpop$ACT 利用attach(data frame名稱) 可省去每次打前面data frame的時間
9
On-line Help可以查詢指令用法help()
> austpop$year NULL > austpop$Year [1] On-line Help可以查詢指令用法help() > help(plot)
10
將excel資料檔案austpop.csv讀入R
> austpop1<-read.csv("c:/RData/austpop.csv") > austpop1 Year NSW Vic. Qld SA WA Tas. NT ACT Aust
11
建立data frame 上面資料是鬆緊帶的資料 stretch-拉的長度 distance-彈出的距離
12
建立data frame > elasticband <- data.frame(stretch=c(46,54,48,50,44,42,52), distance=c(148,182,173,166,109,141,166)) > elasticband stretch distance
13
在編輯視窗編輯資料 > elasticband <- edit(elasticband)
14
付出最多的人,也是收穫最多的人 ~共勉之~
Similar presentations