資料讀取與寫入
# --- 讀取 ----------------------------------------------------------------------------------------------------------------
test.data = read.table(header=T,text="
apple
orange
grape
")
○ apple、 orange、 grape 用來顯示有讀到資料
# --- 寫入 ---------------------------------------------------------------------------------------------------------------
write.table(test.data,file="fruits.txt",sep="")
test.data
# ---- header (TRUE & FALSE 差異) ---------------------------------------------------------------------------
header 為 TRUE
> color.data = read.table(header=T,text="
+ NO. color # header 為 TRUE 此行會被當成標題
+ 1 red
+ 2 blue
+ 3 green
+ ") # 讀取資料
> write.table(color.data,file="color",sep="") # 將檔案寫入資料夾(存檔)
> color.data # 秀出檔案內容
NO. color
1 1 red
2 2 blue
3 3 green
header 為 FALSE
> colors.data = read.table(header=F,text="
+ NO. color # header 為 FALSE 此行會被當作資料
+ 1 red
+ 2 blue
+ 3 green
+ ")
>
> write.table(colors.data,file="color",sep="")
> colors.data
V1 V2 # 標題
1 NO. color
2 1 red
3 2 blue
4 3 green
____________________________________________________________________________________________________
# 流程控制
※ 與 JAVA 寫法相似
# if...else
> x= 5
> if(x>3){
+ print('True')
+ }else{
+ print('False')
+ }
[1] "True"
# else if
> x= 10
> if(x>30){
+ print('True')
+ }else if(x<5) {
+ print('False')
+ }else {
+ print('Center')
+ }
[1] "Center"
> # FOR 迴圈
> for(i in 1:5){
+ print(i)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> sum=0
> for(i in 1:5){
+ sum =sum+i
+ }
> sum
[1] 15
> x <-c("sunny","cloudy","rainy","cloudy","rainy")
>
> for (i in 1:length(x)){
+ print(x[i])
+ }
[1] "sunny"
[1] "cloudy"
[1] "rainy"
[1] "cloudy"
[1] "rainy"
> for (i in seq_along(x)){
+ print(x[i])
+ }
[1] "sunny"
[1] "cloudy"
[1] "rainy"
[1] "cloudy"
[1] "rainy"
> for (letter in x){
+ print(letter)
+ }
[1] "sunny"
[1] "cloudy"
[1] "rainy"
[1] "cloudy"
[1] "rainy"
> # FOR 雙重迴圈
> mat = matrix(1:9,byrow=TRUE,nrow=3)
> for(i in seq_len(nrow(mat))){
+ for(j in seq_len(nrow(mat))) {
+ print(mat[i,j])
+ }
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
> mat
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> # while 迴圈,不符while定義的條件會跳出迴圈
> sum = 0
> num = 0
> while(num <= 50){
+ sum = sum+num;
+ num = num+1;
+ }
> sum
[1] 1275
> #Repeat 會一直執行不中斷
> sum = 0
> num = 0
>
> repeat{
+ num = num+1;
+ if(num > 50)break;
+ sum = sum+num
+ }
> sum
[1] 1275
# --- 練習題 ─ 九九乘法表 ---------------------------------------------------------------------------------------------
# 九九乘法表 (迴圈寫法)
mat = matrix(1:81, byrow= TRUE,nrow=9)
for(i in seq_len(nrow(mat))){
for(j in seq_len(ncol(mat))){
mat[i,j]=i*j
}
}
mat
# 九九乘法表 (矩陣寫法)
mat1=matrix(1:9)
mat2=matrix(1:9,nrow=1)
mat =mat1%*%mat2
mat
___________________________________________________
#九九乘法表 ─ teacher ans 迴圈寫法
for (i in 1:9){
for (j in 1:9){
print(sprintf("%d X %d = %d",i,j,i*j))
}
}
#九九乘法表 ─ teacher ans 矩陣寫法
m1 = matrix(1:9, nrow=9, byrow=TRUE)
m2 = matrix(1:9, nrow=1, byrow=TRUE)
m1%*%m2
_____________________________________________________________________________________________
# 函式 function
> f = function(a,b){ # function( 參數)
+ a*5 # 敘述 (要做的事)
+ }
> f(3,) # 將3代入a,b沒給值
[1] 15
> f = function(a,b){
+ a*b
+ }
> f(3,)
Error in f(3, ) : argument "b" is missing, with no default # b沒給值,無法計算
> f = function(a,b){
+ a*b
+ }
> f(5,7)
[1] 35
# --- function ANS ---------------------------------------------------------------------------------------------
match_func = function (filename="match.txt")
match_func =read.table(file="match.txt")
match_func
f = read.table("match.txt",sep="|") # sep 表示取代
f
m = matrix(-1,nrow= 5,ncol=5)
m
rownames(m)= c("A","B","C","D","E") # 設列的名稱
colnames(m)= c("A","B","C","D","E") # 設行的名稱
m
m[2,3]
m["A","C"]
# m[2,3] = 印出第2列第3行
# m["A","C"] = 印出第A列第C行
for(i in 1:nrow(f) ){
m[f[i,1],f[i,2]] = f[i,3]
}
# for
# i,1 = 第一行 A
# i,2 = 第二行 B
# i,3 = 第三行 1
# 1:nrow = 1到N列
相關參考資訊來源: 邱老師教學
沒有留言:
張貼留言