Matrix ကို r ဖြင့် vector သို့ပြောင်းနည်း (ဥပမာများနှင့်အတူ)


မက်ထရစ်ကို R တွင် vector တစ်ခုသို့ ပြောင်းရန် အောက်ပါ syntax ကို သင်သုံးနိုင်သည်။

 #convert matrix to vector (sorted by columns) using c()
new_vector <- c(my_matrix)

#convert matrix to vector (sorted by rows) using c()
new_vector <- c(t(my_matrix))

#convert matrix to vector (sorted by columns) using as.vector()
new_vector <- as. vector (my_matrix)

#convert matrix to vector (sorted by rows) using as.vector()
new_vector <- as. vector (t(my_matrix))

c() နှင့် as.vector() လုပ်ဆောင်ချက်များသည် တူညီသောရလဒ်များကို ပြန်ပေးမည်ကို သတိပြုပါ။

အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ matrix ဖြင့် ဤလုပ်ဆောင်ချက်တစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသသည်-

 #create matrix
my_matrix <- matrix(1:20, nrow = 5 )

#display matrix
my_matrix

     [,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20

ဥပမာ 1- c() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ Matrix ကို Vector သို့ ပြောင်းပါ (ကော်လံများဖြင့် စီထားခြင်း)

အောက်ပါကုဒ်သည် c() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ မက်ထရစ်အား vector (ကော်လံများဖြင့် စီထားသည်) သို့ မည်သို့ပြောင်းလဲရမည်ကို ပြသသည်-

 #convert matrix to vector (sorted by columns)
new_vector <- c(my_matrix)

#display vector
new_vector

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

ဥပမာ 2- c() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ Matrix ကို Vector သို့ ပြောင်းပါ (အတန်းများအလိုက် စီထားသည်)

အောက်ပါကုဒ်သည် c() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ မက်ထရစ်အား vector (အတန်းများအလိုက် စီထားသည့်) သို့ ပြောင်းလဲနည်းကို ပြသသည်-

 #convert matrix to vector (sorted by rows)
new_vector <- c(t(my_matrix))

#display vector
new_vector

[1] 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20

ဥပမာ 3- as.vector() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ Matrix ကို Vector သို့ ပြောင်းပါ (ကော်လံများဖြင့် စီထားသည်)

အောက်ပါကုဒ်သည် as.vector() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ မက်ထရစ်အား vector (ကော်လံများဖြင့် စီထားသည့်) သို့ မည်သို့ပြောင်းလဲရမည်ကို ပြသသည်-

 #convert matrix to vector (sorted by columns)
new_vector <- as. vector (my_matrix)

#display vector
new_vector

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

ဥပမာ 4- as.vector() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ မက်ထရစ်ကို vector (အတန်းအလိုက် စီထားသည်) သို့ ပြောင်းပါ

as.vector() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ အောက်ပါကုဒ်သည် မက်ထရစ်အား vector (အတန်းအလိုက် စီထားသည်) သို့ မည်သို့ပြောင်းလဲရမည်ကို ပြသသည်-

 #convert matrix to vector (sorted by rows)
new_vector <- as. vector (t(my_matrix))

#display vector
new_vector

[1] 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20

ထပ်လောင်းအရင်းအမြစ်များ

စာရင်းတစ်ခုအား R တွင်ဒေတာဘောင်တစ်ခုသို့မည်သို့ပြောင်းရမည်နည်း။
R တွင် အက္ခရာကို ဂဏန်းအဖြစ် ပြောင်းနည်း
ဇာတ်ကောင်တစ်ခုအား R တွင်အချက်တစ်ခုသို့ဘယ်လိုပြောင်းရမည်နည်း။

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်