○ 陣列中只能放相同屬性的值
若更改其中一個值的型態則會整個陣列的值都一起改掉
例: 設一陣列,並將其中一個數值改成字串型態其結果為?
> matrix(1:9, nrow=3) # 設一陣列
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> mat = matrix(1:9, nrow=3) # 將陣列指派給mat
> mat # 秀出mat 確認
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> mat[1,2]='Q'
> mat
[,1] [,2] [,3]
[1,] "1" "Q" "7"
[2,] "2" "5" "8"
[3,] "3" "6" "9"
● 最後輸出結果 從數值變成字串型態
# -------產生陣列------------------------------------------------------------------------------------
> matrix(1:6,byrow=TRUE,nrow=3)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
byrow = TRUE,表示數值以行為主,先填完行再填下一列
> matrix(1:6,nrow=3)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> matrix(1:6,byrow=FALSE,nrow=3)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
byrow = FALSE,表示數值以列為主,先填完列再填下一行
#-------建立陣列內容----------------------------------------------------------------------------------------
> Danny = c(88,92)
> Anita = c(90,96)
> David = c(78,86)
>
> mat = matrix(c(Danny,Anita,David),nrow=3,byrow=TRUE)
> mat
[,1] [,2]
[1,] 88 92
[2,] 90 96
[3,] 78 86
#-------新增列與欄位名稱----------------------------------------------------------------------------------
方法A
> colnames(mat) = c("chiness","English")
> rownames(mat) = c("Danny","Anita","David")
> mat
chiness English
Danny 88 92
Anita 90 96
David 78 86
方法B
> mat2 = matrix(c(Danny,Anita,David),nrow=3,byrow=TRUE,
+ dimnames=list(c('Danny','Anita','David'),c('chiness','English')))
> mat2
chiness English
Danny 88 92
Anita 90 96
David 78 86
> dim(mat2)
[1] 3 2
#----取列數-----------------------------------------------------------------------------------------
> nrow(mat2)
[1] 3
#----取行數-----------------------------------------------------------------------------------------
> ncol(mat2)
[1] 2
_______________________________________________________________________
====對照表========
> mat2
chiness English
Danny 88 92
Anita 90 96
David 78 86
#----取第一列的所有元------------------------------------------------------------------------------
> mat2[1,]
chiness English
88 92
#----取第一行的所有元素----------------------------------------------------------------------------
> mat2[,1]
Danny Anita David
88 90 78
#----取第一、二列行的所有元素-------------------------------------------------------------------
> mat2[1:2,]
chiness English
Danny 88 92
Anita 90 96
#----取第二列第二行的第二個元素-----------------------------------------------------------------
> mat2[2:2,2]
[1] 96
#----取第二列第一行的所有元素--------------------------------------------------------------------
> mat2[2,1]
[1] 90
mat2 = matrix(c(Kevin,Marry,Jerry),nrow=3,byrow=TRUE,
dimnames=list(c('Kevin','Marry','Jerry'),c('first','second')))
mat2
#-----補充-------------------------------------------------------------------------------------------------
○ 自動產生1~20的向量
> a =1:20
> a
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
○ 秀出10~15
> a[10:15]
[1] 10 11 12 13 14 15
○ 秀出10~25
> a[10:25]
[1] 10 11 12 13 14 15 16 17 18 19 20 NA NA NA NA NA
<不存在的數會以NA表示>
○ 10~20每隔兩號跳號
> a[seq(10,20,2)]
[1] 10 12 14 16 18 20
#------新增列----------------------------------------------------
> mat3=rbind(mat2,c(84,94)) #新增分數
> mat3
chiness English
Danny 88 92
Anita 90 96
David 78 86
84 94
○rbind ,以矩陣型式增加列
> rownames(mat3)[nrow(mat3)]='sam' #新增姓名
> mat3
chiness English
Danny 88 92
Anita 90 96
David 78 86
sam 84 94
#------新增行----------------------------------------------------
> mat4=cbind(mat2,c(96,86,82)) # 在mat2新增一行分數
> colnames(mat4)[ncol(mat4)]='math'
> mat4
chiness English math
Danny 88 92 96
Anita 90 96 86
David 78 86 82
> mat4=cbind(mat3,c(96,86,82)) # 在mat3新增一行分數
Warning message: # 在mat3 有四列而新增的行數只有三,所以出現Warning message:
In cbind(mat3, c(96, 86, 82)) :
number of rows of result is not a multiple of vector length (arg 2)
> colnames(mat4)[ncol(mat4)]='math'
> mat4
chiness English math
Danny 88 92 96
Anita 90 96 86
David 78 86 82
sam 84 94 96 # 因為沒有第四行數,所以將第一行的數再拿過來使用
#--------rowSums & colSums-----------------------------------
# 列‧加總
> rowSums(mat4)
Danny Anita David sam
276 272 246 274
# 行‧加總
> colSums(mat4)
chiness English math
340 368 360
#------矩陣運算----------------------------------------------------
> m1 = matrix(1:4,byrow=TRUE,nrow=2)
> m1
[,1] [,2]
[1,] 1 2
[2,] 3 4
> m2 = matrix(5:8,byrow=TRUE,nrow=2)
> m2
[,1] [,2]
[1,] 5 6
[2,] 7 8
> m1+m2
[,1] [,2]
[1,] 6 8
[2,] 10 12
> m1-m2
[,1] [,2]
[1,] -4 -4
[2,] -4 -4
> m1*m2
[,1] [,2]
[1,] 5 12
[2,] 21 32
> m1/m2
[,1] [,2]
[1,] 0.2000000 0.3333333
[2,] 0.4285714 0.5000000
> m1%%m2
[,1] [,2]
[1,] 1 2
[2,] 3 4
#------矩陣乘積----------------------------------------------------
m1 X m2 (交叉相乘法)
m1%*%m2
> m1%*%m2
[,1] [,2]
[1,] 19 22
[2,] 43 50
#------矩陣轉置----------------------------------------------------
○ 將列&行的擺放順序交換
====對照表========
> m1 = matrix(1:4,byrow=TRUE,nrow=2)
> m1
[,1] [,2]
[1,] 1 2
[2,] 3 4
> m2 = matrix(5:8,byrow=FALSE,nrow=2)
> m2
[,1] [,2]
[1,] 5 7
[2,] 6 8
#------t( )------------------------------------------------------------
> t(m1)
[,1] [,2]
[1,] 1 3
[2,] 2 4
> t(m2)
[,1] [,2]
[1,] 5 6
[2,] 7 8
#-練習題---ANS----------------------------------------------
Q:總成績為加權第一科成績佔40%,加權第一科成績佔60%,試用矩陣運算完成
> Danny = c(88,92)
> Anita = c(90,96)
> David = c(78,86)
>
> mat = matrix(c(Danny,Anita,David),nrow=3,byrow=TRUE)
> mat
方法1
mat[,1]*0.4+mat[,2]*0.6
cbind (mat, mat[,1]*0.4+mat[,2]*0.6)
方法2
mat %*% c(0.4,0.6)
________________________________________________________________________________________
階層 Factor
#---將資料轉換為類別資料 factor( )---------------------------------------------------
> weather= c("sunny","rainy","cloudy","rainy","cloudy") # category 種類
> weather = factor(weather)
> weather
[1] sunny rainy cloudy rainy cloudy
Levels: cloudy rainy sunny
#---有順序的階層 order()-----------------------------------------------------------------
> temperature = c("Low","High","High","Medium","Low","Medium")
> temperature_category = factor(temperature, order=TRUE,
+ levels= c("Low","Medium","High"))
> temperature_category
[1] Low High High Medium Low Medium
Levels: Low < Medium < High
> temperature_category[3] > temperature_category[1] # High > Low
[1] TRUE
> temperature_category[4] > temperature_category[3] # Medium > High
[1] FALSE
● (補充) sort(x)是對向量x進行排序,返回值排序後的數值向量。
> sort(temperature_category)
[1] Low Low Medium Medium High High # 依序排列
Levels: Low < Medium < High
● (補充) rank( ) .........代補
#---觀看類別(種類) levels( )-----------------------------------------------------------------
> levels(weather_category)
[1] "cloudy" "rainy" "sunny"
> levels(temperature_category)
[1] "Low" "Medium" "High"
#---快速轉換類別名稱------------------------------------------------------------------------
> weather = c("s","r","c","r","c") # 將內容簡寫
> weather_factor =factor(weather) # 設新變數將factor屬性的值存進去
> weather # 印出weather
[1] "s" "r" "c" "r" "c" # 秀出內容
> weather_factor # 印出weather_factor
[1] s r c r c # 成功轉換
Levels: c r s # 總共有3種類別
> levels(weather_factor) = c("cloudy","rainy","sunny") # 將內容改寫為全名
> weather_factor # 印出weather_factor
[1] sunny rainy cloudy rainy cloudy # 成功轉換為全名
Levels: cloudy rainy sunny # 總共有3種類別
#----練習題------ANS------------------------------------------------------------
> air_class= c("business","economy","first","economy","first")
方法1
> air_factor = factor(air_class)
> air_factor
[1] business economy first economy first
Levels: business economy first
方法2
> levels(factor(air_class))
[1] "business" "economy" "first"
方法3
> unique(air_class)
[1] "business" "economy" "first"
沒有留言:
張貼留言