-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote5_Function_H24071126葉嘉浤.Rmd
194 lines (190 loc) · 4.57 KB
/
Note5_Function_H24071126葉嘉浤.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "Note_Function_H24071126葉嘉浤"
author: "H24071126葉嘉浤"
date: "2020/5/4"
output: html_document
---
# 1.Source
## 1.寫好程式碼
## 2.將程式碼存為XXX.R檔,並用source執行程式碼
## 3.用print或cat才會輸出最後的結果
```{}
*print => 顯示的字元會用引號隔開
*cat => 顯示的字元不用引號,可用"\n"將字元換行
```
```{r,out.width= 450}
knitr::include_graphics("./source.jpg")
#用source執行程式碼
knitr::include_graphics("./source_output.jpg")
```
```{r}
x <- c("A", "B", "C", "D")
print(x)
cat(x)
#用"\n"將字元換行
cat(x[1], "\n",
x[2], "\n", sep = "" )
#找出完美數(使用print)
k <- 1
while(k < 100){
fac <- c()
for(j in 1:k){
if((k %% j) == 0) fac <- c(fac, j)
}
if(k == sum(fac[-length(fac)])){
out <- paste(paste(k, "=", sep=" "),
paste(fac[-length(fac)], collapse = " + "),
sep = " ")
print(out)
}
k <- k+1
}
#找出完美數(使用cat)
k <- 1
while(k < 100){
fac <- c()
for(j in 1:k){
if((k %% j) == 0) fac <- c(fac, j)
}
if(k == sum(fac[-length(fac)])){
cat(paste(k, "=", sep=" "),
paste(fac[-length(fac)], collapse = " + "), "\n")
}
k <- k+1
}
```
# 2.function(一個輸入值)
```{r}
f <- function(x){
3*x + 2
}
f(2); f(3); f(4)
#舉例
make.percent <- function(x){
x.percent <- round(100*x, digits = 1)
x.show <- paste(x.percent, "%", sep = "")
cat(x.show)
}
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
ans <- make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
ans
#輸出為NULL,因為函數值未貼到到ans這個變數
```
## Return
### 將參數透過算式計算後回傳到函式
```{r}
f <- function(x){
return(3*x + 2)
}
f(2)
f <- function(x){
res <- 3*x + 2
}
f(2)
#因為函數算出來的值沒有回傳到函式,所以f(2)未輸出任何結果
#舉例(使用return)
make.percent <- function(x){
x.percent <- round(100*x, digits = 1)
x.show <- paste(x.percent, "%", sep = "")
return(x.show)
}
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
ans <- make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
ans
#舉例(使用invisible)
make.percent <- function(x){
x.percent <- round(100*x, digits = 1)
x.show <- paste(x.percent, "%", sep = "")
invisible(x.show)
}
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
#使用invisible不會直接顯示計算結果,需要再命名一個新的變數來輸出結果
ans <- make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
ans
```
## 函數名稱
### 函數名稱被視為一個物件
### 如果有向量與函數的名稱相同,該名稱的類型為最近使用的類型
```{r}
objA <- function(x){
return(2*x)
}
objA #輸出為一個函數
objA <- objA(4)
objA #輸出為一個向量
```
# 3.function(多個輸入值)
```{r}
f <- function(x, y){
x^2 + y^2
}
f(2, 2); f(2, 3); f(2, 4)
#舉例
make.percent <- function(x, x.digits){
x.percent <- round(100*x, digits = x.digits)
x.show <- paste(x.percent, "%", sep = "")
return(x.show)
}
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736), 0)
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736), 1)
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736), 2)
#參數設定一個預設值
make.percent <- function(x, x.digits = 0){
x.percent <- round(100*x, digits = x.digits)
x.show <- paste(x.percent, "%", sep = "")
return(x.show)
}
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736))
make.percent(c(0.8933, 0.2344, 0.0764, 0.7736), 0)
```
# 4.用function和return輸出多個結果
## list:一種能存放很多不同類型物件的向量
```{r}
X <- list(a = 3, b = c(1,2,3,4,5), c = c("A", "B"))
X
```
### 用 $ 或 [[ ]] 提取list中的元素
$:元素的名稱
[[ ]]:元素的位置或名稱
```{r}
X <- list(a = 3, b = c(1,2,3,4,5), c = c("A", "B"))
X$a
X[[2]]
X[["c"]]
```
建立list
```{r}
create <- list()
length(create)
length(create) <- 2
create[[1]] <- seq(1,5,2)
create
create$plus <- seq(1,5,2)+3 #新命名的物件會放在list的最後面
create
create[[2]]
create[[3]]
names(create)
length(create) #list的長度因為新增一個名為plus的物件所以+1
#舉例
perfect.number <- function(a, b){
k <- a
res <- c()
ans <- list()
while(k < b){
fac <- c()
for(j in 1:k){
if((k %% j) == 0) fac <- c(fac, j)
}
if(k == sum(fac[-length(fac)])) {
res <- c(res, k)
ans[[paste(k)]] <- fac[-length(fac)]
}
k <- k+1
}
ans$number <- res
return(ans)
}
Ans <- perfect.number(1, 100)
Ans
Ans[["28"]]
```