ဖော်မတ်ချထားသော စာကြောင်းများကို ရိုက်နှိပ်ရန် r တွင် sprintf လုပ်ဆောင်ချက်ကို မည်သို့အသုံးပြုရမည်နည်း။
ဖော်မတ်ချထားသော စာကြောင်းများကို ရိုက်နှိပ်ရန် R တွင် sprintf() လုပ်ဆောင်ချက်ကို သင် အသုံးပြုနိုင်သည်။
ဤလုပ်ဆောင်ချက်သည် အောက်ပါအခြေခံ syntax ကိုအသုံးပြုသည်-
sprintf(fmt၊x)
ရွှေ-
- fmt : အသုံးပြုရန် ဖော်မတ်
- x : ဖော်မတ်လုပ်ရန် တန်ဖိုး
အောက်ဖော်ပြပါ ဥပမာများသည် ဤလုပ်ဆောင်ချက်ကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။
ဥပမာ 1- ဒဿမအမှတ်ပြီးနောက် ဂဏန်းများကို ဖော်မတ်ပါ။
ဒဿမနေရာနှစ်ခုကိုသာပြသရန် အောက်ပါကုဒ်သည် sprintf() ကို အသုံးပြုပုံကိုပြသသည်-
#define value x <- 15.49347782 #only display 2 digits after decimal place sprintf(" %2f ", x) [1] "15.49"
ဥပမာ 2- ဒဿမအမှတ်ရှေ့တွင် ဂဏန်းများဖော်မတ်ပါ။
ဒဿမအမှတ်မတိုင်မီ ဂဏန်းဆယ်လုံးပြသရန် အောက်ပါကုဒ်သည် sprintf() ကို အသုံးပြုနည်းကို ပြသသည်-
#define value x <- 15435.4 #display 10 total digits before decimal place sprintf(" %10.f ", x) [1] "15435"
ဒဿမအမှတ်မတိုင်မီ ဂဏန်းငါးလုံးသာရှိသောကြောင့် sprintf() လုပ်ဆောင်ချက်သည် ဒဿမအမှတ်မတိုင်မီ စုစုပေါင်းဂဏန်း 10 လုံးရရန် string ၏အစသို့ နောက်ထပ်နေရာငါးခုကို ထပ်ပေါင်းထည့်သည်။
ဥပမာ 3- သိပ္ပံဆိုင်ရာ အမှတ်အသားကို အသုံးပြု၍ တန်ဖိုးကို ပုံစံဖော်ပါ။
သိပ္ပံနည်းကျမှတ်စုတွင် တန်ဖိုးတစ်ခုပြသရန် အောက်ပါကုဒ်သည် sprintf() ကိုအသုံးပြုပုံကို ပြသသည်-
#define value x <- 15435.4 #display in scientific notation using lowercase e sprintf(" %e ", x) [1] "1.543540e+04" #display in scientific notation using uppercase E sprintf(" %E ", x) [1] "1.543540E+04"
ဥပမာ 4- လိုင်းတစ်ခုတွင် တန်ဖိုးတစ်ခုကို ဖော်မတ်ပါ။
အောက်ပါကုဒ်သည် string တစ်ခုသို့တန်ဖိုးတစ်ခုဖော်မတ်ရန် sprintf() ကိုအသုံးပြုပုံကိုပြသသည်-
#define value x <- 5.4431 #display string with formatted value sprintf(" I rode my bike about %.1f miles ", x) [1] “I rode my bike about 5.4 miles”
ဥပမာ 5: string တစ်ခုတွင် တန်ဖိုးများစွာကို ဖော်မတ်လုပ်ပါ။
အောက်ပါကုဒ်သည် string တစ်ခုသို့တန်ဖိုးများစွာကိုဖော်မတ်ရန် sprintf() ကိုအသုံးပြုပုံကိုပြသသည်-
#define values x1 <- 5.4431 x2 <- 10.778342 #display string with formatted values sprintf(" I rode my bike%%1f miles and then ran%%2f miles ", x1, x2) [1] “I rode my bike 5.4 miles and then ran 10.78 miles”
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် R တွင် အခြားဘုံလုပ်ဆောင်ချက်များကို အသုံးပြုနည်းကို ရှင်းပြထားပါသည်။
R တွင် paste နှင့် paste0 လုပ်ဆောင်ချက်များကိုအသုံးပြုနည်း
R တွင် replace() လုပ်ဆောင်ချက်ကို အသုံးပြုနည်း
R တွင် View() function ကိုအသုံးပြုနည်း