2. R 統計軟體發展歷史
? R 統計軟體最初是由Ross Ihaka及Robert
Gentleman兩人以統計分析及繪圖為目的,
仿S語言的架構為基礎而發展出來的統計軟
體,可視為改進版本的S語言。大部分的S語
言程式碼可直接或稍做修改後就在R上面執
行
? R屬於GNU計畫中的一個項目,目前是由
R Development Core Team維護及發展
? 目前R最新的版本為3.1.1版(2014.07.10)
6. 軟體下載
? Google 搜尋 “R” 第一個顯示即是R統
計軟體網頁
? The R Project for Statistical
Computing
? CRAN
? 選擇下載點
:http://cran.csie.ntu.edu.tw/
? Windows ?base?R-3.0.0-win32.exe
32. useful commands on vectors
sample(x, 4) # randomly picks four states
sample(x) # randomly permute the entire vector
of state names
sample(x, replace=TRUE) # selection with
replacement
33. useful commands on vectors
rev(x) # reverses the vector
sum(x) # sums all the elements in a numeric or logical vector
cumsum(x) # returns a vector of cumulative sums (or a
running total)
diff(x) # returns a vector of differences between adjacent
elements
max(x) # returns the largest element
min(x) # returns the smallest element
range(x) # returns a vector of the smallest and largest
elements
mean(x) # returns the arithmetic mean
42. list()
? Lists can hold data structures of different
types, and of different sizes.
? Each component in a list can be (optionally,
but commonly) separately named.
43. list()
? A list of Peter O’Toole
pete <- list("Peter", "O'Toole", 1932, FALSE)
print(pete)
pete <- list(first.name = "Peter", last.name =
"O'Toole", yob = 1932, oscar.winner = FALSE)
print(pete)
44. list()
m1 <- list(title = "Lawrence of Arabia", year = 1962)
m2 <- list(title = "Stardust", year = 2007)
m3 <- list(title = "Troy", year = 2004)
pete$movies <- list(m1, m2, m3)
pete[["roles"]] <- c("T.E. Lawrence", "King", "Priam")
str(pete)
45. Data frame
? Data frames are two-dimensional data
structures like matrices, but unlike matrices
can have multiple different data types.
? You can think of a data frame as a list of
vectors, where all the vector lengths are the
same.
47. Data frame
library(foreign)
x=read.spss("d:/somedir/x4.sav") # not a data.frame
names(mydata)
#"id" "score" "age"
mydata2=data.frame(id=x$id,score=x$score,age=x$age)
library(R.matlab)
dataMat <- readMat("my_matlab_file.mat")
data <-data.frame(dataMat$var)