R ဖြင့် အချိုးများကို တွက်ချက်နည်း (ဥပမာများဖြင့်)


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

နည်းလမ်း 1- Base R ကိုသုံးပါ။

 #calculate ratio between variable1 and variable2
df$ratio <- df$variable1/df$variable1

#calculate ratio between variable1 and variable2, rounded to 2 decimal places
df$ratio <- round(df$variable1/df$variable2, 2 )

နည်းလမ်း 2: dplyr ကိုသုံးပါ။

 library (dplyr)

#calculate ratio between variable1 and variable2
df <- df %>%
        mutate(ratio = variable1/variable2)

#calculate ratio between variable1 and variable2, rounded to 2 decimal places
df <- df %>%
        mutate(ratio = round(variable1/variable2, 2 ))

ဤသင်ခန်းစာတွင် မတူညီသော ဘတ်စကက်ဘောကစားသမားများမှ ရိုက်ယူခဲ့သည့် စုစုပေါင်းရိုက်ချက်အရေအတွက်ကို ပြသသည့် အောက်ပါဒေတာဘောင်ဖြင့် လက်တွေ့တွင် နည်းလမ်းတစ်ခုစီကို အသုံးပြုပုံကို ရှင်းပြထားသည်။

 #create data frame
df <- data. frame (players=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 makes=c(4, 4, 3, 6, 7, 8, 3, 10),
                 attempts=c(12, 7, 5, 6, 10, 12, 5, 19))

#view data frame
df

  players makes attempts
1 to 4 12
2 B 4 7
3 C 3 5
4 D 6 6
5 E 7 10
6 F 8 12
7 G 3 5
8:10:19 a.m.

ဥပမာ 1- R အခြေခံကို အသုံးပြု၍ အချိုးများကို တွက်ချက်ပါ။

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

 #calculate ratio between makes and attempts columns
df$ratio <- df$makes/df$attempts

#view updated data frame
df

  players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8:10:19 AM 0.5263158

ပထမကစားသမားအတွက် ကြိုးစားမှု အချိုးသည် 4/12 = 0.33 ဖြစ်သည်

တစ်နည်းဆိုရသော် ပထမကစားသမားသည် ၎င်း၏ ပစ်ခတ်မှု၏ ၃၃ ရာခိုင်နှုန်း ခန့်ကို ပြုလုပ်ခဲ့သည်။

ကျွန်ုပ်တို့သည် အခြားကစားသမားအားလုံးအတွက် အချိုးတန်ဖိုးများကို အလားတူနည်းလမ်းဖြင့် အဓိပ္ပာယ်ဖွင့်ဆိုနိုင်ပါသည်။

ဒဿမနေရာအချို့သို့ အဝိုင်းအချိုးတန်ဖိုးများကို round() function ကိုလည်း အသုံးပြုနိုင်သည်။

 #calculate ratio between makes and attempts columns, rounded to 2 decimal places
df$ratio <- round(df$makes/df$attempts, 2 )

#view updated data frame
df

  players makes attempts ratio
1 to 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8:10:19 0.53

အချိုး ကော်လံရှိ တန်ဖိုးတစ်ခုစီကို ဒဿမနေရာနှစ်ခုသို့ ဝိုင်းထားသည်။

ဥပမာ 2- dplyr ကို အသုံးပြု၍ အချိုးများကို တွက်ချက်ပါ။

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

 library (dplyr)

#add new column that shows ratio of makes to attempts
df <- df %>%
        mutate(ratio = makes/attempts)

#view updated data frame
df

  players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8:10:19 AM 0.5263158

ဒဿမနေရာအချို့သို့ အဝိုင်းအချိုးတန်ဖိုးများကို round() function ကိုလည်း အသုံးပြုနိုင်သည်။

 library (dplyr)

#add new column that shows ratio of makes to attempts, rounded to 2 decimal places
df <- df %>%
        mutate(ratio = round(makes/attempts, 2 ))

#view updated data frame
df

  players makes attempts ratio
1 to 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8:10:19 0.53

အချိုး ကော်လံရှိ တန်ဖိုးတစ်ခုစီကို ဒဿမနေရာနှစ်ခုသို့ ဝိုင်းထားသည်။

အခြေခံ R နည်းလမ်းနှင့် dplyr နည်းလမ်းသည် တူညီသောရလဒ်များကို ထုတ်ပေးကြောင်း သတိပြုပါ။

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

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

dplyr ကို အသုံးပြု၍ ထူးခြားသောတန်ဖိုးများကို စစ်ထုတ်နည်း
dplyr ကို အသုံးပြု၍ အခြေအနေများစွာဖြင့် စစ်ထုတ်နည်း
R ရှိ ကော်လံများတွင် ဖြစ်ပွားမှုအရေအတွက်ကို ရေတွက်နည်း

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

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