R Matrices
Matrices
မက်ထရစ်သည် ကော်လံများနှင့် အတန်းများပါရှိသော ဘက်မြင်နှစ်ဘက်ဆိုင်ရာဒေတာအတွဲတစ်ခုဖြစ်သည်။
ကော်လံသည် ဒေတာ၏ ဒေါင်လိုက်ကိုယ်စားပြုမှုဖြစ်ပြီး အတန်းသည် ဒေတာ၏အလျားလိုက်ကိုယ်စားပြုမှုဖြစ်သည်။
လုပ်ဆောင်ချက် ဖြင့် matrix ကို ဖန်တီးနိုင်သည် matrix()
။ အတန်းများနှင့် ကော်လံများ၏ ပမာဏကို ရယူရန် nrow
နှင့် ဘောင်များကို သတ်မှတ် ပါ-ncol
ဥပမာ
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
# Print the
matrix
thismatrix
မှတ်ချက်-c()
အရာများကို ပေါင်းစည်းရန် လုပ်ဆောင်ချက်ကို အသုံးပြုကြောင်း မှတ်သား ထားပါ။
သင်သည် strings များနှင့်အတူ matrix ကိုဖန်တီးနိုင်သည်။
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix
Matrix Items များကို အသုံးပြုပါ။
ကွင်းစကွင်းပိတ် များကို အသုံးပြု၍ အရာများကို သင်ဝင်ရောက်နိုင်သည် [ ]
။ ကွင်းစကွင်းပိတ်ရှိ ပထမနံပါတ် "1" သည် အတန်းတည်နေရာကို သတ်မှတ်ပေးသည်၊ ဒုတိယနံပါတ် "2" သည် ကော်လံ-အနေအထားကို သတ်မှတ်သည်-
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[1, 2]
ကွင်းစကွင်းပိတ်ရှိ နံပါတ် ပြီးနောက် ကော်မာတစ်ခု သတ်မှတ်ပါက အတန်းတစ်ခုလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်သည် -
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[2,]
ကွင်းစကွင်းပိတ်ရှိ နံပါတ် ရှေ့တွင် ကော်မာတစ်ခု သတ်မှတ်ပါက ကော်လံတစ်ခုလုံးကို ဝင်ရောက်ကြည့်ရှုနိုင်သည် -
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[,2]
အတန်းတစ်ခုထက်ပို၍ ဝင်ရောက်ပါ။
လုပ်ဆောင်ချက်ကို သင်အသုံးပြုပါက အတန်းတစ်ခုထက်ပို၍ ဝင်ရောက်နိုင်သည် c()
-
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[c(1,2),]
ကော်လံတစ်ခုထက်ပို၍ ဝင်ရောက်ကြည့်ရှုပါ။
လုပ်ဆောင်ချက်ကို သင်အသုံးပြုပါက ကော်လံတစ်ခုထက်ပို၍ ဝင်ရောက်နိုင်သည် c()
-
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[,
c(1,2)]
အတန်းများနှင့် ကော်လံများ ထည့်ပါ။
cbind()
Matrix တွင် နောက်ထပ်ကော်လံများထည့်ရန် လုပ်ဆောင်ချက်ကို အသုံးပြု ပါ။
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print the new matrix
newmatrix
မှတ်ချက်- ကော်လံအသစ်ရှိဆဲလ်များသည် လက်ရှိ matrix နှင့် တူညီသောအရှည်ဖြစ်ရမည်။
rbind()
Matrix တွင် နောက်ထပ်အတန်းများထည့်ရန် လုပ်ဆောင်ချက်ကို အသုံးပြု ပါ။
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print the new matrix
newmatrix
မှတ်ချက်- အတန်းသစ်ရှိဆဲလ်များသည် လက်ရှိ matrix နှင့် တူညီသောအရှည်ဖြစ်ရမည်။
အတန်းများနှင့် ကော်လံများကို ဖယ်ရှားပါ။
c()
Matrix ရှိ အတန်းများနှင့် ကော်လံများကို ဖယ်ရှားရန် လုပ်ဆောင်ချက်ကို အသုံးပြု ပါ-
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"),
nrow = 3, ncol =2)
#Remove the first row and the first column
thismatrix <- thismatrix[-c(1), -c(1)]
thismatrix
ပစ္စည်းတစ်ခုရှိမရှိ စစ်ဆေးပါ။
သတ်မှတ်ထားသော အရာတစ်ခုသည် matrix တစ်ခုတွင် ရှိနေခြင်းရှိမရှိ သိရှိရန်၊ %in%
အော်ပရေတာအား အသုံးပြုပါ-
ဥပမာ
matrix တွင် "apple" ရှိမရှိစစ်ဆေးပါ။
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
"apple" %in% thismatrix
အတန်းများနှင့် ကော်လံများ ပမာဏ
dim()
Matrix တစ်ခုရှိ အတန်းများနှင့် ကော်လံများ၏ ပမာဏကို ရှာဖွေရန် လုပ်ဆောင်ချက်ကို အသုံးပြု ပါ။
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
dim(thismatrix)
Matrix အရှည်
length()
Matrix ၏အတိုင်းအတာကိုရှာဖွေရန် function ကိုသုံးပါ
ဥပမာ
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
length(thismatrix)
မက်ထရစ်ရှိ စုစုပေါင်းဆဲလ်များသည် ကော်လံအရေအတွက်ဖြင့် မြှောက်ထားသော အတန်းအရေအတွက်ဖြစ်သည်။
အထက်ဖော်ပြပါ ဥပမာတွင်- Dimension = 2*2 = 4 ။
Matrix မှတဆင့် Loop
Loop ကို အသုံးပြု၍ Matrix ကို လှ ည့် for
ပတ်နိုင်သည်။ ကွင်းဆက်သည် ပထမအတန်းတွင် စတင်မည်ဖြစ်ပြီး ညာဘက်သို့ရွှေ့ပါမည်-
ဥပမာ
မက်ထရစ်အကြောင်းအရာများကို လှည့်ပတ်ပြီး ပရင့်ထုတ်ပါ-
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
for (rows in 1:nrow(thismatrix)) {
for (columns in
1:ncol(thismatrix)) {
print(thismatrix[rows, columns])
}
}
Matrices နှစ်ခုကို ပေါင်းစပ်ပါ။
တစ်ဖန်၊ သင်သည် matrices နှစ်ခု သို့မဟုတ် ထို့ထက်ပိုသော matrices များကို ပေါင်းစပ်ရန် rbind()
သို့မဟုတ် function ကိုသုံးနိုင်သည်-cbind()
ဥပမာ
# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry",
"grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango",
"pineapple", "watermelon"), nrow = 2, ncol = 2)
# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined