R ကြိုးများ


စာကြောင်းများ

စာလုံးတစ်လုံး သို့မဟုတ် စာကြောင်းများကို စာသားသိမ်းဆည်းရန်အတွက် အသုံးပြုသည်။ စာကြောင်းတစ်ခုအား ကိုးကားအမှတ်အသားတစ်ခု သို့မဟုတ် နှစ်ထပ်ကိုးကားအမှတ်များဖြင့် ဝန်းရံထားသည်-

"hello"အတူတူပါပဲ 'hello':

ဥပမာ

"hello"
'hello'

ကိန်းရှင်တစ်ခုသို့ String တစ်ခုသတ်မှတ်ပါ။

<-variable တစ်ခုသို့ string တစ်ခုအား သတ်မှတ်ခြင်းသည် operator နှင့် string တို့၏ နောက်တွင်ရှိသော variable ဖြင့် လုပ်ဆောင်သည် -

ဥပမာ

str <- "Hello"
str # print the value of str

Multiline ကြိုးများ

သင်သည် ဤကဲ့သို့သော variable တစ်ခုသို့ multiline string ကို သတ်မှတ်ပေးနိုင်ပါသည်။

ဥပမာ

str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."

str # print the value of str

သို့သော် မျဉ်းကြောင်းတစ်ခုစီ၏အဆုံးတွင် R သည် " \n " ကို ထည့်မည်ကို သတိပြုပါ ။ ၎င်းကို escape character ဟုခေါ်ပြီး n အက္ခရာသည် စာကြောင်းအသစ်ကို ညွှန်ပြသည်

ကုဒ်ရှိ မျဉ်းကြောင်းများအတိုင်း တူညီသော အနေအထားတွင် ထည့်သွင်းလိုပါက cat()လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-

ဥပမာ

str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."

cat(str)


ကြိုးအရှည်

R တွင် အသုံးဝင်သော string function များစွာရှိသည်။

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

ဥပမာ

str <- "Hello World!"

nchar(str)

ကြိုးတစ်ချောင်းကို စစ်ဆေးပါ။

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

ဥပမာ

str <- "Hello World!"

grepl("H", str)
grepl("Hello", str)
grepl("X", str)

ကြိုးနှစ်ချောင်းကို ပေါင်းစပ်ပါ။

paste()စာကြောင်းနှစ်ခုကို ပေါင်းစည်းရန်/ပေါင်းစပ်ရန် လုပ်ဆောင်ချက်ကို အသုံးပြု ပါ။

ဥပမာ

str1 <- "Hello"
str2 <- "World"

paste(str1, str2)

Escape ဇာတ်ကောင်များ

စာကြောင်းတစ်ခုတွင်တရားမဝင်သောဇာတ်ကောင်များထည့်ရန်၊ သင်အလွတ်စာလုံးကိုအသုံးပြုရပါမည်။

\Escape ဇာတ်ကောင်သည် သင်ထည့်သွင်းလိုသော ဇာတ်ကောင်၏နောက်တွင် backslash တစ်ခုဖြစ်သည် ။

တရားမဝင် စာလုံးတစ်လုံး၏ ဥပမာသည် ကိုးကားနှစ်ထပ်ဖြင့် ဝန်းရံထားသော စာကြောင်းအတွင်း၌ နှစ်ထပ်ကိုးကားချက်ဖြစ်သည်-

ဥပမာ

str <- "We are the so-called "Vikings", from the north."

str

ရလဒ်:

Error: unexpected symbol in "str <- "We are the so-called "Vikings"

ဤပြဿနာကိုဖြေရှင်းရန် Escape စာလုံးကိုအသုံးပြုပါ \"

ဥပမာ

ပုံမှန်အားဖြင့် သင် ခွင့်မပြုသောအခါတွင် Escape Character သည် သင့်အား ကိုးကားနှစ်ထပ်ကို အသုံးပြုခွင့်ပေးသည်-

str <- "We are the so-called \"Vikings\", from the north."

str
cat(str)

str variable ကို အလိုအလျောက်ပုံနှိပ်ခြင်းသည် output တွင် backslash ကို print ထုတ်မည်ကို သတိပြုပါ ။ cat()backslash မပါဘဲ ပရင့်ထုတ်ရန် လုပ်ဆောင်ချက်ကို သင်သည် အသုံးပြုနိုင်သည် ။

R တွင် အခြားထွက်ပြေးသည့်ဇာတ်ကောင်များ

Code Result
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace