Dplyr တွင် cross() function ကိုအသုံးပြုနည်း (ဥပမာ 3 ခု)


ကော်လံအများအပြားသို့ အသွင်ပြောင်းခြင်းကို အသုံးပြုရန် R ရှိ dplyr ပက်ကေ့ခ်ျမှ cross() လုပ်ဆောင်ချက်ကို သင်အသုံးပြုနိုင်ပါသည်။

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

နည်းလမ်း 1- ကော်လံများစွာတွင် လုပ်ဆောင်ချက်တစ်ခုကို အသုံးပြုပါ။

 #multiply values in col1 and col2 by 2
df %>% 
  mutate(across(c(col1, col2), function (x) x*2))

နည်းလမ်း 2- ကော်လံများစွာအတွက် အကျဉ်းချုပ်စာရင်းအင်းကို တွက်ချက်ပါ။

 #calculate mean of col1 and col2
df %>%
  summarise(across(c(col1, col2), mean, na. rm = TRUE ))

နည်းလမ်း 3- ကော်လံများစွာအတွက် Multiple Summary Statistics ကို တွက်ချက်ပါ။

 #calculate mean and standard deviation for col1 and col2
df %>%
  summarise(across(c(col1, col2), list(mean=mean, sd=sd), na. rm = TRUE ))

အောက်ပါဥပမာများသည် အောက်ပါဒေတာဘောင်ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-

 #create data frame
df <- data. frame (conf=c('East', 'East', 'East', 'West', 'West', 'West'),
                 points=c(22, 25, 29, 13, 22, 30),
                 rebounds=c(12, 10, 6, 6, 8, 11))

#view data frame
df

  conf points rebounds
1 East 22 12
2 East 25 10
3 East 29 6
4 West 13 6
5 West 22 8
6 West 30 11

ဥပမာ 1- ကော်လံများစွာတွင် လုပ်ဆောင်ချက်တစ်ခုကို အသုံးပြုပါ။

အောက်ဖော်ပြပါ ကုဒ်သည် အမှတ် များရှိ တန်ဖိုးများကို မြှောက်ပြီး ကော်လံများကို 2 ဖြင့် ပြန်လှန်ရန် cross() လုပ်ဆောင်ချက်ကို အသုံးပြုနည်းကို ပြသသည်-

 library (dplyr)

#multiply values in points and rebounds columns by 2
df %>% 
  mutate(across(c(points, rebounds), function (x) x*2))

  conf points rebounds
1 East 44 24
2 East 50 20
3 East 58 12
4 West 26 12
5 West 44 16
6 West 60 22

ဥပမာ 2- ကော်လံများစွာအတွက် အကျဉ်းချုပ်ကိန်းဂဏန်းကို တွက်ချက်ပါ။

အောက်ဖော်ပြပါ ကုဒ်သည် အမှတ်များ၏ ပျမ်းမျှတန်ဖိုးကို တွက်ချက်ရန်နှင့် ကော်လံများကို ပြန်လှန်ရန်အတွက် cross() လုပ်ဆောင်ချက်ကို အသုံးပြုပုံကို ပြသသည်-

 library (dplyr)

#calculate mean value of points an rebounds columns
df %>%
  summarise(across(c(points, rebounds), mean, na. rm = TRUE ))

  rebound points
1 23.5 8.833333

ဒေတာဘောင်ရှိ ကိန်းဂဏာန်းကော်လံများအားလုံးအတွက် အကျဉ်းချုပ်စာရင်းအင်းတစ်ခုကို အလိုအလျောက်တွက်ချက်ရန် is.numeric လုပ်ဆောင်ချက်ကိုလည်း အသုံးပြုနိုင်ကြောင်း သတိပြုပါ။

 library (dplyr)

#calculate mean value for every numeric column in data frame
df %>%
  summarise(across(where(is. numeric ), mean, na. rm = TRUE ))

  rebound points
1 23.5 8.833333

ဥပမာ 3- ကော်လံများစွာအတွက် အနှစ်ချုပ်ကိန်းဂဏန်းများစွာကို တွက်ချက်ပါ။

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

 library (dplyr)

#calculate mean and standard deviation for points and rebounds columns
df %>%
  summarise(across(c(points, rebounds), list(mean=mean, sd=sd), na. rm = TRUE ))

  points_mean points_sd rebounds_mean rebounds_sd
1 23.5 6.156298 8.833333 2.562551

မှတ်ချက်cross() လုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာနိုင်သည်။

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

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

dplyr ကို အသုံးပြု၍ အတန်းများကို ဖျက်နည်း
dplyr ကို အသုံးပြု၍ အတန်းများကို မည်သို့စီစဉ်မည်နည်း။
dplyr ကို အသုံးပြု၍ အခြေအနေများစွာဖြင့် စစ်ထုတ်နည်း

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

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