• 沒有找到結果。

# 常用指令函數

s an(): 實數資料可利用"複製,"張貼快捷鍵 。(字串或類別變數只能用 () 指令或paste() 指令) 先將欲輸入的資料 (變數間隔為空格) 利用快捷鍵"複製起來, 於作業欄中輸入

>1:"張貼資料''

### ←֓

(完成) 函數di(x): 階差函數一般用法 di(x, lag= 2)

### ai , i = 1, 2, 3, · · ·

di(x,lag =2) 表數列

### ai+2− ai , i = 1, 2, 3, · · ·

> diff(x,lag=2)

[1℄ 1 2 3 5 8 13 21 34 55

(答):01 12 3 58 1321 34

seq函數表建立首項為 from,末項to(

### ≤

to), 公差 by 的數列。

[1℄ 0 1 2 3 4 5 6 7 8 9 10

> seq(0,10,length=5)

[1℄ 0.0 2.5 5.0 7.5 10.0

> seq(1, 9, by = pi)

[1℄ 1.000000 4.141593 7.283185

>

[1℄ 1 2 3 4 1 2 3 4

> rep(1:4, ea h=2)

[1℄ 1 1 2 2 3 3 4 4

> rep(1:4, (2,2,2,2))

[1℄ 1 1 2 2 3 3 4 4

> a

[1℄ NaN

> is.nan(a)

[1℄ TRUE

> b <- log(0)

> b

[1℄ -Inf

> is.finite(b)

[1℄ FALSE

> x <- (0:4,NA,5:7)

> x

[1℄ 0 1 2 3 4 NA 5 6 7

> is.na(x)

[1℄ FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE

> x[!is.na(x)℄ # 去除 NA(錯誤值) 的資料 (只留存正確數值資料)

[1℄ 0 1 2 3 4 5 6 7

>x[!is.na(x)℄[x[!is.na(x) ℄>4. 5℄ #符合數值資料>4.5 的資料

[1℄ 5 6 7

paste(..., ollapse="", sep ="")其中參數 ollapse: 用於分隔結果的可選字符串。sep:

> A

[1℄ "a" "b" " "

> paste("a","b",sep="")

[1℄ "ab"

> paste(A, ("d","e"))

[1℄ "a d" "b e" " d"

> paste(A, ("d","e"), ollaps e=", ",se p=", ")

[1℄ "a,d,b,e, ,d"

> paste(A, ("d","e"), ollaps e="" ,sep ="," )

[1℄ "a,db,e ,d"

> paste(A, ("d","e"), ollaps e="" ,sep ="")

> paste("$",sep="",A) [1℄ "$a" "$b" "$ "

> paste(A,1:5,sep="")

[1℄ "a1" "b2" " 3" "a4" "b5"

> A <- ("a","b"," ","a","a"," " ,"B" );ta ble( A) #字串資料統計表

A

>rownames(A)= ("r1","r2" ,"r3 ","r 4") #設定矩陣列名稱

#也可用 dimnames(A)[[1℄℄= ("r1"," r2", "r3" ,"列名稱") 設定第1維度 各行列名稱

> olnames(A)= (" 1"," 2" ," 3 "," 4"," 5") #設定矩陣行名稱

#也可用 dimnames(A)[[2℄℄= (" 1"," 2"," 3", " 4" ,"行名" ) 設定第2

> A2 <- matrix(1:20,4,5,byrow = TRUE)

> A2

> A3 <- matrix(1:20,5,4,byrow = TRUE) #同 A^t

> A3

> A[ (1,4), (2,3)℄ #subse tioning and modifying subse tion

2 3 #保留 A 矩陣的第1,4列及第2,3行的元素矩陣

> A[4,,drop = FALSE℄

1 2 3 4 5

[3,℄ 9 21 33 45 57

[4,℄ 16 32 48 64 80

> A/x #Entry-wise division: 對應除法為依照行依序除以 x 運算

[,1℄ [,2℄ [,3℄ [,4℄ [,5℄

[1,℄ 1 5.000000 9.000000 13 17.000000

[2,℄ 1 3.000000 5.000000 7 9.000000

[3,℄ 1 2.333333 3.666667 5 6.333333

[4,℄ 1 2.000000 3.000000 4 5.000000

> A <- matrix( (2,3,5,4),nrow=2,n ol= 2)

> B <- matrix( (1,2,2,7),nrow=2,n ol= 2)

> A

#lambda=7 為 (-0.7071068,-0.7071068);la mbda =-1 為 (-0.8574929,0.5144958)

[,1℄ [,2℄

[1,℄ -0.7071068 -0.8574929

[2,℄ -0.7071068 0.5144958

solve(A)及解方程組:

,

### X = A−1B

=solve(A,B)

> A <- matrix( (2,3,5,4),nrow=2,n ol= 2)

> B <- matrix( (1,2,2,7),nrow=2,n ol= 2)

> A

[1,℄ 2.3333333 -0.6666667

[2,℄ -0.6666667 0.3333333

> rossprod(A,B) # t(A)%*%B # Cross produ t:

### A T B

[,1℄ [,2℄

[1,℄ 8 25

[2,℄ 13 38

> C=A%*%B

> solve(A,C) # AB=C solve B=

### A −1 C

[,1℄ [,2℄

[1,℄ 1 2

[2,℄ 2 7

array 函數以"逐行的方式將data 填入陣列,並持多維度的向量數據資料。2維度的array 陣列等同於matrixarray 陣列函數其列、 行名稱用法同 matrix 用法。

> x <- matrix(1:10, 2)

> y <- array(1:10, (2, 5))

> identi al(x, y)

[1℄ TRUE

rbind、 bindarray 建立陣列 array 變數

n ol、nrow 陣列的橫列數、 直行數

row、 olumn 傳回元素所在的列指標或行指標值

rownames、 olnames 查詢或更改橫列名稱、 直行名稱

dim 查詢維度

dimnames 查詢或設定各維度的行列名稱

aperm 或 t() 陣列轉置; 轉置矩陣

Data frames: 建立數據資料架構 (類似 Ex ele)R 自動依序指派

### 1 ∼ n

ma-trix() 所有元素為相同屬性。 在相同的資料下由 data.frame() 建構的型態, 物件所需記憶

> gender <- fa tor( ("m","m","f"))

> lass1 <- (60,60,80)

> lass2 <- (40,50,30)

> grades <- data.frame(name,gender, lass 1, l ass2 )

name gender lass1 lass2

1 john m 60 40

> x= ("Yes","No","No","Yes ","Y es")

> x # print out values in x

[1℄ "Yes" "No" "No" "Yes" "Yes"

> table(x)

x

No Yes

2 3

> fa tor(x) # print out value in fa tor(x)

[1℄ Yes No No Yes Yes

[1,℄ 1 6 11

>no= (seq(1:7));height= ( 68,6 1,63 ,70, 69,6 5,72 );

weight= (155,99,115,205,1 70,1 25,2 20);

>A= bind(no,height,weight )

>weight= (155,99,115,205,1 70, 125, 220) ;

> x

[1℄ 68 61 63 70 69 65 72

no height weight

> as.data.frame(A) # as.data.frame(A): 將矩陣資料型態轉成資料框架變數

no height weight

> sum(A[,2℄)/length(A[,2℄) # 求身高平均值

[1℄ 66.42857

> A

[,1℄ [,2℄

r1 "a" "b"

r2 "3" "4 #矩陣資料所有元素必須同一屬性

> r1= (1,2);r2= (3,4);A=rbind (r1, r2)

> A

[,1℄ [,2℄

r1 1 2

r2 3 4

>r1= (1,2);r2= (3,4);Y= b ind( r1,r 2)

> Y

3. 您在您的汽車加滿油並記錄了最近八次里程數:65311 65624 65908 66219 66499 6682167145

67447

4. 用 R語言造出以下數列:

(a) 用rep 指令造出以下數列:1 11 1 12 22 2 33 3 44 5

(b) 用seq 指令造出以下數列:12 3 45 62 3 45 6 34 5 64 56 5 66

( ) 用 rep 指令造出以下數列: "A" "A" "A" "A" "A" "B" "B" "B" "B" "C" "C" "C"

"D" "D" "E"

(d) 用 , rep 指令造出以下數列:

"東" "" "" "" "西" "西" "西" "" "" ""

(e) 用 , seq, letters指令造出以下數列:

"b" "d" "f" "h" "j" "l" "n" "p" "r" "t" "v" "x" "z" "a" " " "e" "g" "i" "k" "m"

"o" "q" "s" "u" "w" "y"

(f) 產生數列

### 2 , 13 , − 14 , · · · , − 101

5. 用 R語言造出以下數列:

(a) "a" , "a" , "a" , "a", "a" .

(b)

### 1 ∼ 100

( ) 1,2, 3,4, 5, 4,3,2, 1

6. 用 R語言 seq() 指令

(a) 造出首項為 3,公差為2的等差數列50?

(b) 輸入 seq(50,from=3,by=2 ) , 預期輸出的結果如何? 共幾項

( ) 輸入指令 seq(1,100,length=30) , 預期輸出的結果如何?

(d) 分別輸入指令seq(1,10,by=0.7);seq(10,1 ,by= -0.7 ) , 得到的最大值與最小值為何?

7. 用 s an() 指令造出下列"數列:(實數資料利用"複製,"張貼快捷鍵)

(a) 1 23 12 3 12 3

(b) 10.0000010.0454510.0909110.1363610.1818210.2272710.2727310.3181810.36364

10.4090910.4545510.50000

8. 造出下列"數列:

(a) "1" "2" "3" "banana" "1" "2" "3" "banana"

(b) 用 paste 指令造出: "A1" "A2""A3" "B1" "B2""C1" "C2" "C3" "C4" "D1""D2"

"E1"

( ) 用paste指令造出:"student_1" "student_2" "student_3" "student_4" "student_5"

9. 在 R中鍵入下列指令, 會輸出什麼結果?

(a) paste( ("a","b"), (" ","d") , ol laps e=" ",se p="" )

(b) strsplit( ("ab; d"),";")

( ) strsplit( ("ab; d"),";")

(d) substring("ab def",3, (3,5, 4))

10. 您可以追踪兩週 (十天) 的上學通勤時間,並以分鐘為單位記錄以下時間:171620 242215 21

Student Ques. 1 Ques. 2 Ques. 3

1 3 5 1

(b) >Mat<-matrix(1:12,nrow=4,by row= TRUE );M at[3 ,℄;M at[2 ,2:3 ℄

( ) matrix( (1,0,0,1),nrow=2)*m atri x(1: 4,n row= 2)

14. 給出指令以創建3階單位矩陣?

2 Julia green 160

( ) 用rownames 指令設定橫列名稱:"r1","r2", "r3","r4"

(d) 用 olnames 指令設定行名稱:" ol1"," ol2"," ol3"

(e) 將資料my.mat 的列(row) 命名為 no.1,no.2, no.3, no.4,將欄 ( olumn) 命名為var.1,

(e) > whi h(m == 1,arr.ind=TRUE)

(f) 將矩陣 m中非2的元素全部更改為0

7 58 191 M

;

### Y

(a) 用 bind 指令, 建立 X 矩陣

(b) 若矩陣乘積

### (XtX)B = XtY

,solve 指令及矩陣運算解出 B 矩陣?

29. 任意輸入兩數值 a,b, 並從螢幕列印出"您輸入 a,b 值各為?" 並顯示出

Outline