R တွင် သတ်သတ်မှတ်မှတ် ဇာတ်ကောင်တစ်ခုပြီးနောက် ကြိုးတစ်ချောင်းကို မည်သို့ထုတ်မည်နည်း။
R တွင် သီးခြားစာလုံးတစ်ခုပြီးနောက် string တစ်ခုကို ထုတ်ယူရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
နည်းလမ်း 1- Base R ကို အသုံးပြု၍ သီးခြားစာလုံးများပြီးနောက် စာကြောင်းကို ထုတ်ယူပါ။
sub(' .*the ', '', my_string)
နည်းလမ်း 2- stringr ကို အသုံးပြု၍ သီးခြားစာလုံးများပြီးနောက် string ကို ထုတ်ယူပါ။
library (stringr) str_replace(my_string, ' (.*?)the(.*?) ', ' \\1 ')
ဤဥပမာနှစ်ခုစလုံးသည် my_string ရှိ “ the” ပုံစံပြီးနောက် string ကို ဖြည်သည်။
အောက်ပါဥပမာများသည် အောက်ပါဒေတာဘောင်ဖြင့် နည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးပြုနည်းကို ပြသသည်-
#create data frame
df <- data. frame (team=c('theMavs', 'theHeat', 'theNets', 'theRockets'),
dots=c(114, 135, 119, 140))
#view data frame
df
team points
1 theMavs 114
2 theHeat 135
3 theNets 119
4 theRockets 140
ဥပမာ 1- Base R ကို အသုံးပြု၍ သီးခြားစာလုံးများပြီးနောက် string တစ်ခုကို ထုတ်ယူပါ။
အောက်ပါကုဒ်သည် ဒေတာဘောင်၏ အဖွဲ့ ကော်လံရှိ အတန်းတစ်ခုစီအတွက် “ the” ပြီးနောက် စာကြောင်းကို မည်သို့ထုတ်ယူရမည်ကို ပြသသည်-
#create new column that extracts string after "the" in team column df$team_name <- sub(' .*the ', '', df$team) #view updated data frame df team points team_name 1 theMavs 114 Mavs 2 theHeat 135 Heat 3 theNets 119 Nets 4 theRockets 140 Rockets
team_name ဟုခေါ်သော ကော်လံအသစ်တွင် ဒေတာဘောင်ရှိ အဖွဲ့ ကော်လံရှိ အတန်းတစ်ခုစီအတွက် “ the” ပြီးနောက် စာကြောင်းပါရှိသည်။
ဆက်စပ် – R တွင် sub() ၏ နိဒါန်း
ဥပမာ 2- stringr ပက်ကေ့ချ်ကို အသုံးပြု၍ သီးခြားစာလုံးများပြီးနောက် string တစ်ခုကို ထုတ်ယူပါ။
အောက်ပါကုဒ်သည် R ရှိ stringr ပက်ကေ့ဂျ်မှ str_replace() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ဒေတာဘောင်၏ အဖွဲ့ ကော်လံရှိ အတန်းတစ်ခုစီအတွက် “ the” ပြီးနောက် string ကို မည်သို့ထုတ်ယူရမည်ကို ပြသသည်-
library (stringr) #create new column that extracts string after "the" in team column df$team_name <- str_replace(df$team, ' (.*?)the(.*?)', '\\1 ') #view updated data frame df team points team_name 1 Mavs pro team 114 Mavs 2 team Heat pro 135 Heat 3 Nets pro team 119 Nets
team_name ဟုခေါ်သော ကော်လံအသစ်တွင် ဒေတာဘောင်ရှိ အဖွဲ့ ကော်လံရှိ အတန်းတစ်ခုစီအတွက် “ the” ပြီးနောက် စာကြောင်းပါရှိသည်။
၎င်းသည် base R ရှိ sub() function ကိုအသုံးပြုခြင်း၏ရလဒ်များနှင့် ကိုက်ညီပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် R တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
R တွင် သတ်မှတ်ထားသော စာကြောင်းတစ်ခုပါရှိသော ကော်လံများကို မည်သို့ရွေးချယ်ရမည်နည်း။
R တွင် string တစ်ခုမှ ဇာတ်ကောင်များကို မည်ကဲ့သို့ ဖယ်ရှားနည်း
R တွင် string တစ်ခုရှိ ဇာတ်ကောင်တည်နေရာကို မည်သို့ရှာရမည်နည်း။