R ရှိ စာကြောင်းများမှ space များကို မည်ကဲ့သို့ ဖယ်ရှားနည်း (ဥပမာ 3 ခု)


R ရှိ စာကြောင်းများမှ နေရာလွတ်များကို ဖယ်ရှားရန် အောက်ပါနည်းလမ်းများကို သင်သုံးနိုင်သည်။

နည်းလမ်း 1- gsub() ကို အသုံးပြု၍ Whitespace အားလုံးကို ဖယ်ရှားပါ

 updated_string <- gsub(" ", "", my_string)

နည်းလမ်း 2- str_replace_all() ကို အသုံးပြု၍ နေရာလွတ်များအားလုံးကို ဖယ်ရှားပါ

 library (stringr)

updated_string <- str_replace_all(my_string, " ", "")

နည်းလမ်း 3- str_trim() ကို အသုံးပြု၍ ရှေ့ဆောင်နှင့် နောက်လိုက်နေရာများကို ဖယ်ရှားပါ

 library (stringr)

#remove all trailing whitespace
updated_string <- str_trim(my_string, " right ")

#remove all leading whitespace
updated_string <- str_trim(my_string, " left ")

အောက်ဖော်ပြပါ ဥပမာများသည် နည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။

ဥပမာ 1- gsub() ကို အသုံးပြု၍ space အားလုံးကို ဖယ်ရှားပါ

အောက်ပါကုဒ်သည် ပေးထားသည့် စာကြောင်းမှ နေရာလွတ်အားလုံးကို ဖယ်ရှားရန် R ရှိ gsub() လုပ်ဆောင်ချက်ကို မည်သို့အသုံးပြုရမည်ကို ပြသသည်-

 #create string
my_string <- "Check out this cool string"

#remove all whitespace from string
updated_string <- gsub(" ", "", my_string)

#view updated string
updated_string

[1] "Checkoutthiscoolstring"

နေရာလွတ်အားလုံးကို စာကြောင်းမှ ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။

ဥပမာ 2- str_replace_all() ကို အသုံးပြု၍ Space အားလုံးကို ဖယ်ရှားပါ

အောက်ဖော်ပြပါကုဒ်သည် ပေးထားသည့် စာကြောင်းမှ နေရာလွတ်အားလုံးကို ဖယ်ရှားရန် R ရှိ stringr အထုပ်၏ str_replace_all() လုပ်ဆောင်ချက်ကို မည်သို့အသုံးပြုရမည်ကို ပြသသည်-

 library (stringr)

#create string
my_string <- "Check out this cool string"

#remove all whitespace from string
updated_string <- str_replace_all(my_string, " ", "")

#view updated string
updated_string

[1] "Checkoutthiscoolstring"

နေရာလွတ်အားလုံးကို စာကြောင်းမှ ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။

ဥပမာ 3- str_trim() ကို အသုံးပြု၍ ရှေ့ဆောင်နှင့် နောက်လိုက်နေရာများကို ဖယ်ရှားပါ

အောက်ဖော်ပြပါကုဒ်သည် ပေးထားသည့် string တစ်ခုမှ ဦးဆောင်နေရာလွတ်များအားလုံးကို ဖယ်ရှားရန် R ရှိ stringr package ၏ str_trim() လုပ်ဆောင်ချက်ကို မည်သို့အသုံးပြုရမည်ကို ပြသသည်-

 library (stringr)

#create string with leading whitespace
my_string <- "Check out this cool string"

#remove all leading whitespace from string
updated_string <- str_trim(my_string, " left ")

#view updated string
updated_string

[1] “Check out this cool string”

ဦးဆောင်နေရာများအားလုံးကို ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။

အောက်ပါကုဒ်သည် ပေးထားသည့် စာကြောင်းတစ်ခုမှ နောက်ဆက်တွဲနေရာများအားလုံးကို ဖယ်ရှားရန် str_trim() လုပ်ဆောင်ချက်ကို အသုံးပြုပုံကို ပြသသည်-

 library (stringr)

#create string with trailing whitespace
my_string <- "Check out this cool string "

#remove all trailing whitespace from string
updated_string <- str_trim(my_string, " right ")

#view updated string
updated_string

[1] “Check out this cool string”

နောက်လိုက်နေရာများအားလုံးကို ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။

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

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

R တွင် string တစ်ခုရှိ ဇာတ်ကောင်တည်နေရာကို မည်သို့ရှာရမည်နည်း။
R တွင် ကြိုးများကို ပေါင်းစပ်နည်း
R တွင် vector မှ string ကိုမည်သို့ပြောင်းရမည်နည်း။
ဇာတ်ကောင်တစ်ခုအား R တွင်အချက်တစ်ခုသို့ဘယ်လိုပြောင်းရမည်နည်း။

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

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