R တွင် attach() ကိုအသုံးပြုနည်း (ဥပမာများဖြင့်)
ဒေတာဘောင်အမည်ကို ရိုက်ထည့်စရာမလိုဘဲ ဒေတာဘောင်အရာဝတ္ထုများကို အသုံးပြုနိုင်ရန် R တွင် attach() လုပ်ဆောင်ချက်ကို သင်အသုံးပြုနိုင်ပါသည်။
ဤလုပ်ဆောင်ချက်သည် အောက်ပါအခြေခံ syntax ကိုအသုံးပြုသည်-
attach(data)
အောက်ပါဥပမာများသည် အောက်ပါဒေတာဘောင်ဖြင့် ဤလုပ်ဆောင်ချက်ကို မတူညီသောအခြေအနေများတွင် မည်သို့အသုံးပြုရမည်ကို ပြသသည်-
#create data frame df <- data. frame (team=c('A', 'B', 'C', 'D', 'E'), points=c(99, 90, 86, 88, 95), assists=c(33, 28, 31, 39, 34), rebounds=c(30, 28, 24, 24, 28)) #view data frame df team points assists rebounds 1 A 99 33 30 2 B 90 28 28 3 C 86 31 24 4 D 88 39 24 5 E 95 34 28
ဥပမာ 1- တွက်ချက်မှုများလုပ်ဆောင်ရန် attach() ကိုသုံးပါ။
ပုံမှန်အားဖြင့် ပျမ်းမျှ၊ အလယ်အလတ်၊ အပိုင်းအခြား စသည်တို့ကို တွက်ချက်လိုပါက၊ ဒေတာဘောင်တစ်ခုအတွင်းရှိ ကော်လံတစ်ခု၏ အောက်ပါ syntax ကို ကျွန်ုပ်တို့ အသုံးပြုပါမည်။
#calculate mean of rebounds column
mean(df$rebounds)
[1] 26.8
#calculate median of rebounds column
median(df$rebounds)
[1] 28
#calculate range of rebounds column
range(df$rebounds)
[1] 24 30
သို့သော်၊ ကျွန်ုပ်တို့သည် attach() ကို အသုံးပြုပါက၊ ဤတွက်ချက်မှုများကိုလုပ်ဆောင်ရန် data frame name ကိုပင်ထည့်သွင်းရန်မလိုအပ်ပါ။
attach(df)
#calculate mean of rebounds column
mean(rebounds)
[1] 26.8
#calculate median of rebounds column
median(rebounds)
[1] 28
#calculate range of rebounds column
range(rebounds)
[1] 24 30
attach() ကိုအသုံးပြုခြင်းဖြင့်၊ ကျွန်ုပ်တို့သည် ကော်လံအမည်ကို တိုက်ရိုက်ကိုးကားနိုင်ပြီး ကျွန်ုပ်တို့အသုံးပြုရန်ကြိုးစားနေသည့် မည်သည့်ဒေတာဘောင်ကို R သိနိုင်သည်။
ဥပမာ 2- ဆုတ်ယုတ်မှုပုံစံများနှင့်ကိုက်ညီရန် attach() ကိုသုံးပါ။
ပုံမှန်အားဖြင့်၊ R တွင် linear regression model ကို အံဝင်ခွင်ကျဖြစ်စေလိုပါက၊ အောက်ပါ syntax ကို အသုံးပြုပါမည်။
#fit regression model
fit <- lm(points ~ assists + rebounds, data=df)
#view coefficients of regression model
summary(fit)$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) 18.7071984 13.2030474 1.416885 0.29222633
assists 0.5194553 0.2162095 2.402555 0.13821408
rebounds 2.0802529 0.3273034 6.355733 0.02387244
သို့သော်၊ အကယ်၍ ကျွန်ုပ်တို့သည် attach() ကို အသုံးပြုပါက၊ regression model နှင့်ကိုက်ညီရန် lm() function တွင် data argument ကိုအသုံးပြုရန်မလိုအပ်ပါ။
#fit regression model
fit <- lm(points ~ assists + rebounds)
#view coefficients of regression model
summary(fit)$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) 18.7071984 13.2030474 1.416885 0.29222633
assists 0.5194553 0.2162095 2.402555 0.13821408
rebounds 2.0802529 0.3273034 6.355733 0.02387244
ဆုတ်ယုတ်မှုရလဒ်များသည် အတိအကျတူညီကြောင်း သတိပြုပါ။
အပိုဆု- detach() နှင့် search() ကိုသုံးပါ
လက်ရှိ R ပတ်ဝန်းကျင်တွင် ပူးတွဲပါရှိသော အရာအားလုံးကို ပြသရန် search() လုပ်ဆောင်ချက်ကို သင်အသုံးပြုနိုင်သည်-
#show all attached objects
search()
[1] ".GlobalEnv" "df" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
ထို့အပြင် သင်သည် လက်ရှိ ဖြုတ်ထားသော အရာဝတ္တုကို ဖယ်ရှားရန် detach() လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်သည်။
#detach data frame
detach(df)
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် R တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
R မှာ ပတ်ဝန်းကျင်ကို ဘယ်လိုရှင်းမလဲ။
RStudio ရှိ မြေကွက်များအားလုံးကို မည်ကဲ့သို့ ရှင်းလင်းမည်နည်း။
R တွင် တူညီသောမျဉ်းပေါ်တွင် ကိန်းရှင်များစွာကို မည်သို့ပုံနှိပ်ရမည်နည်း။