R Vectors


ကွက်ကွက်များ

vector သည် အမျိုးအစားတူသောအရာများစာရင်းတစ်ခုဖြစ်သည်။

အရာများစာရင်းကို vector တစ်ခုသို့ ပေါင်းစပ်ရန်၊ c()လုပ်ဆောင်ချက်ကို အသုံးပြုပြီး အရာများကို ကော်မာဖြင့် ပိုင်းခြားပါ။

အောက်ဖော်ပြပါ ဥပမာတွင်၊ ကြိုးများပေါင်းစပ်ထားသော fruits ဟုခေါ်သော vector variable တစ်ခုကို ဖန်တီးသည်-

ဥပမာ

# Vector of strings
fruits <- c("banana", "apple", "orange")

# Print fruits
fruits

ဤဥပမာတွင်၊ ကျွန်ုပ်တို့သည် ကိန်းဂဏာန်းတန်ဖိုးများကို ပေါင်းစပ်ထားသော vector တစ်ခုကို ဖန်တီးသည်-

ဥပမာ

# Vector of numerical values
numbers <- c(1, 2, 3)

# Print numbers
numbers

အစီအစဥ်တစ်ခုတွင် ကိန်းဂဏန်းတန်ဖိုးများရှိသော vector တစ်ခုကို ဖန်တီးရန်၊ :အော်ပရေတာအား အသုံးပြုပါ-

ဥပမာ

# Vector with numerical values in a sequence
numbers <- 1:10

numbers

သင်သည် အစီအစဥ်တစ်ခုတွင် ဒဿမများဖြင့် ဂဏန်းတန်ဖိုးများကို ဖန်တီးနိုင်သည်၊ သို့သော် နောက်ဆုံးဒြပ်စင်သည် အစီစဉ်နှင့်မသက်ဆိုင်ပါက၊ ၎င်းကို အသုံးမပြုကြောင်း သတိပြုပါ။

ဥပမာ

# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1

# Vector with numerical decimals in a sequence where the last element is not used
numbers2 <- 1.5:6.3
numbers2

ရလဒ်:

[1] 1.5 2.5 3.5 4.5 5.5 6.5
[1] 1.5 2.5 3.5 4.5 5.5

အောက်ပါဥပမာတွင်၊ ကျွန်ုပ်တို့သည် ယုတ္တိတန်ဖိုးများ၏ vector တစ်ခုကို ဖန်တီးသည်-

ဥပမာ

# Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)

log_values

Vector အရှည်

Vector တစ်ခုတွင် ပစ္စည်းမည်မျှရှိသည်ကို သိရှိရန်၊ length()လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။

ဥပမာ

fruits <- c("banana", "apple", "orange")

length(fruits)

Vector တစ်ခုကို စီပါ။

vector တစ်ခုတွင် အရာများကို အက္ခရာစဉ် သို့မဟုတ် ဂဏန်းများဖြင့် စီရန်၊ sort()လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-

ဥပမာ

fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)

sort(fruits)  # Sort a string
sort(numbers) # Sort numbers

Vectors များကို ဝင်ရောက်ကြည့်ရှုပါ။

ကွင်းစကွင်းပိတ်များအတွင်းရှိ ၎င်း၏အညွှန်းကိန်းနံပါတ်ကို ရည်ညွှန်းခြင်းဖြင့် သင်သည် vector items များကို ဝင်ရောက်ကြည့်ရှု []နိုင်ပါသည်။ ပထမပစ္စည်းတွင် အညွှန်းကိန်း 1 ၊ ဒုတိယပစ္စည်းတွင် အညွှန်းကိန်း 2 ၊ အစရှိသည်ဖြင့်-

ဥပမာ

fruits <- c("banana", "apple", "orange")

# Access the first item (banana)
fruits[1]

လုပ်ဆောင်ချက် ဖြင့် မတူညီသော အညွှန်းရာထူးများကို ရည်ညွှန်းခြင်းဖြင့် ဒြပ်စင်များစွာကို သင်လည်း ဝင်ရောက်ကြည့်ရှုနိုင်သည် c()-

ဥပမာ

fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access the first and third item (banana and orange)
fruits[c(1, 3)]

သတ်မှတ်ထားသည့်အရာများမှလွဲ၍ အရာအားလုံးကို ဝင်ရောက်ကြည့်ရှုရန် အနုတ်လက္ခဏာကိန်းဂဏန်းများကိုလည်း အသုံးပြုနိုင်ပါသည်။

ဥပမာ

fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access all items except for the first item
fruits[c(-1)]

ပစ္စည်းတစ်ခုပြောင်းပါ။

သီးခြားပစ္စည်းတစ်ခု၏တန်ဖိုးကို ပြောင်းလဲရန် အညွှန်းကိန်းနံပါတ်ကို ကိုးကားပါ-

ဥပမာ

fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Change "banana" to "pear"
fruits[1] <- "pear"

# Print fruits
fruits

Vector များကို ပြန်လုပ်ပါ။

vector များကို ပြန်လုပ်ရန်၊ rep()လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-

ဥပမာ

တန်ဖိုးတစ်ခုစီကို ပြန်လုပ်ပါ-

repeat_each <- rep(c(1,2,3), each = 3)

repeat_each

ဥပမာ

vector ၏ sequence ကိုပြန်လုပ်ပါ

repeat_times <- rep(c(1,2,3), times = 3)

repeat_times

ဥပမာ

တန်ဖိုးတစ်ခုစီကို အမှီအခိုကင်းစွာ ပြန်လုပ်ပါ-

repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))

repeat_indepent

Sequenced Vectors များကို ထုတ်လုပ်ခြင်း။

အပေါ်ရှိ ဥပမာများထဲမှ တစ်ခု၊ :အော်ပရေတာဖြင့် ကိန်းဂဏန်းတန်ဖိုးများ ကိန်းဂဏန်းတန်ဖိုးများရှိသော vector တစ်ခုကို ဖန်တီးနည်းကို ပြသထားသည်-

ဥပမာ

numbers <- 1:10

numbers

အစီအစဥ်တစ်ခုတွင် ပိုကြီးသော သို့မဟုတ် သေးငယ်သောခြေလှမ်းများပြုလုပ်ရန် seq()လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-

ဥပမာ

numbers <- seq(from = 0, to = 100, by = 20)

numbers

မှတ်ချက်_ seq()_ from_ to_by