R တွင် အုပ်စုအလိုက် standard deviation ကို တွက်ချက်နည်း (ဥပမာများဖြင့်)
R တွင် အုပ်စုအလိုက် စံသွေဖည်မှုကို တွက်ချက်ရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်ပါသည်။
Method 1: R base ကိုသုံးပါ။
aggregate(df$col_to_aggregate, list(df$col_to_group_by), FUN=sd)
နည်းလမ်း 2: dplyr ကိုသုံးပါ။
library (dplyr)
df %>%
group_by(col_to_group_by) %>%
summarise_at(vars(col_to_aggregate), list(name=sd))
နည်းလမ်း 3- data.table ကိုသုံးပါ။
library (data.table)
setDT(df)
dt[ ,list(sd=sd(col_to_aggregate)), by=col_to_group_by]
အောက်ပါနမူနာများသည် R ရှိ အောက်ပါဒေတာဘောင်ဖြင့် ဤနည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးပြုနည်းကို ပြသသည်-
#create data frame
df <- data. frame (team=rep(c(' A ', ' B ', ' C '), each= 6 ),
points=c(8, 10, 12, 12, 14, 15, 10, 11, 12,
18, 22, 24, 3, 5, 5, 6, 7, 9))
#view data frame
df
team points
1 to 8
2 to 10
3 to 12
4 to 12
5 to 14
6 to 15
7 B 10
8 B 11
9 B 12
10 B 18
11 B 22
12 B 24
13 C 3
14 C 5
15 C 5
16 C 6
17 C 7
18 C 9
နည်းလမ်း 1- R အခြေခံကို အသုံးပြု၍ အုပ်စုအလိုက် စံသွေဖည်မှုကို တွက်ချက်ပါ။
အသင်းမှရမှတ်များ၏ စံသွေဖည်မှုကို တွက်ချက်ရန် အောက်ပါကုဒ်သည် R ဒေတာဘေ့စ်၏ စုစည်းမှု() လုပ်ဆောင်ချက်ကို မည်သို့အသုံးပြုရမည်ကို ပြသသည်-
#calculate standard deviation of points by team
aggregate(df$points, list(df$team), FUN=sd)
Group.1 x
1 A 2.562551
2 B 6.013873
3 C 2.041241
နည်းလမ်း 2- dplyr ကို အသုံးပြု၍ အုပ်စုအလိုက် စံသွေဖည်မှုကို တွက်ချက်ပါ။
အောက်ပါကုဒ်သည် အဖွဲ့မှရမှတ်များ၏ စံသွေဖည်မှုကို တွက်ချက်ရန်အတွက် group_by () နှင့် summarise_at() လုပ်ဆောင်ချက်များကို dplyr ပက်ကေ့ခ်ျမှ မည်သို့အသုံးပြုရမည်ကို ပြသသည်-
library (dplyr)
#calculate standard deviation of points scored by team
df %>%
group_by(team) %>%
summarise_at(vars(points), list(name=sd))
# A tibble: 3 x 2
team name
1 to 2.56
2 B 6.01
3C 2.04
နည်းလမ်း 3- data.table ကို အသုံးပြု၍ အုပ်စုအလိုက် စံသွေဖည်မှုကို တွက်ချက်ပါ။
အောက်ပါကုဒ်သည် data.table ပက်ကေ့ဂျ်ရှိ လုပ်ဆောင်ချက်များကို အသုံးပြု၍ အဖွဲ့မှ ရမှတ်များ၏ စံသွေဖည်မှုကို တွက်ချက်နည်းကို ပြသသည်-
library (data.table)
#convert data frame to data table
setDT(df)
#calculate standard deviation of points scored by team
df[,list(sd=sd(points)), by=team]
team sd
1: A 2.562551
2: B 6.013873
3:C2.041241
နည်းလမ်းသုံးခုစလုံးသည် တူညီသောရလဒ်များကို ပြန်လာသည်ကို သတိပြုပါ။
မှတ်ချက် – သင်သည် အလွန်ကြီးမားသော ဒေတာဘောင်တစ်ခုဖြင့် အလုပ်လုပ်နေပါက၊ ဤပက်ကေ့ဂျ်များသည် အခြေခံ R ထက် ပိုမိုမြန်ဆန်သောကြောင့် dplyr သို့မဟုတ် data.table ချဉ်းကပ်မှုကို အသုံးပြုရန် အကြံပြုအပ်ပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် R တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
R တွင် အုပ်စုအလိုက် ပျမ်းမျှတွက်နည်း
R တွင် အုပ်စုအလိုက် ပေါင်းစည်းနည်း
R တွင် အုပ်စုအလိုက် အရေအတွက် တွက်ချက်နည်း