R တွင် အတန်းများကို စစ်ထုတ်နည်း
R အတွင်းရှိ အချို့သော အခြေအနေများပေါ်အခြေခံ၍ ဒေတာဘောင်တစ်ခု၏ အပိုင်းခွဲတစ်ခုကို သင်စိတ်ဝင်စားလေ့ရှိပါသည်။ ကံကောင်းထောက်မစွာ၊ ၎င်းသည် dplyr ပက်ကေ့ခ်ျမှ filter() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ လုပ်ဆောင်ရန် လွယ်ကူသည်။
library(dplyr)
ဤသင်ခန်းစာတွင် starwars ဟုခေါ်သော built-in dplyr ဒေတာအစုံကို အသုံးပြု၍ ဤအင်္ဂါရပ်ကို လက်တွေ့ကျကျအသုံးပြုနည်း ဥပမာများစွာကို ရှင်းပြထားသည်။
#view first six rows of starwars dataset
head(starwars)
# A tibble: 6 x 13
name height mass hair_color skin_color eye_color birth_year gender homeworld
1 Luke~ 172 77 blond fair blue 19 male Tatooine
2 C-3PO 167 75 <NA> gold yellow 112 <NA> Tatooine
3 R2-D2 96 32 <NA> white, bl~ red 33 <NA> Naboo
4 Dart~ 202 136 none white yellow 41.9 male Tatooine
5 Leia~ 150 49 brown light brown 19 female Alderaan
6 Owen~ 178 120 brown, gr~ light blue 52 male Tatooine
# ... with 4 more variables: species , films , vehicles ,
# starships
ဥပမာ 1- အချို့သောတန်ဖိုးနှင့်ညီသောအတန်းများကို စစ်ထုတ်ပါ။
အောက်ပါကုဒ်သည် ပြောင်းလဲနိုင်သော “ မျိုးစိတ်များ” သည် Droid နှင့် ညီမျှသည့် အတန်းများအတွက် ဒေတာအတွဲကို စစ်ထုတ်နည်းကို ပြသထားသည်။
starwars %>% filter(species == ' Droid ')
# A tibble: 5 x 13
name height mass hair_color skin_color eye_color birth_year gender homeworld
1 C-3PO 167 75 gold yellow 112 Tatooine
2 R2-D2 96 32 white, bl~ red 33 Naboo
3 R5-D4 97 32 white, red red NA Tatooine
4 IG-88 200 140 none metal red 15 none
5 BB8 NA NA none none black NA none
# ... with 4 more variables: species , films , vehicles ,
# starships
#A tibble: 5 x 13 မှ ညွှန်ပြထားသည့်အတိုင်း dataset ၏ 5 တန်းသည် ဤအခြေအနေနှင့်ကိုက်ညီကြောင်း ကျွန်ုပ်တို့တွေ့မြင်နိုင်ပါသည်။
ဥပမာ 2- “and” ကို အသုံးပြု၍ အတန်းများကို စစ်ထုတ်ပါ
မျိုးစိတ် Droid ရှိရာ အတန်းများကို စစ်ထုတ်နိုင်ပြီး မျက်လုံးအရောင်မှာ အနီရောင် ဖြစ်သည် –
starwars %>% filter(species == ' Droid ' & eye_color == ' red ') # A tibble: 3 x 13 name height mass hair_color skin_color eye_color birth_year gender homeworld 1 R2-D2 96 32 <NA> white, bl~ red 33 <NA> Naboo 2 R5-D4 97 32 <NA> white, red red NA <NA> Tatooine 3 IG-88 200 140 none metal red 15 none <NA> # ... with 4 more variables: species , films , vehicles , # starships
ဒေတာအတွဲတွင် အတန်း ၃ တန်းသည် ဤအခြေအနေနှင့် ကိုက်ညီကြောင်း ကျွန်ုပ်တို့ မြင်နိုင်သည်။
ဥပမာ 3- “သို့မဟုတ်” ကိုအသုံးပြု၍ အတန်းများကို စစ်ထုတ်ပါ
မျိုးစိတ် Droid သို့မဟုတ် မျက်လုံးအရောင် အနီရောင်ရှိသော အတန်းများကိုလည်း စစ်ထုတ်နိုင်သည်-
starwars %>% filter(species == ' Droid ' | eye_color == ' red ') # A tibble: 7 x 13 name height mass hair_color skin_color eye_color birth_year gender homeworld 1 C-3PO 167 75 <NA> gold yellow 112 <NA> Tatooine 2 R2-D2 96 32 <NA> white, bl~ red 33 <NA> Naboo 3 R5-D4 97 32 <NA> white, red red NA <NA> Tatooine 4 IG-88 200 140 none metal red 15 none <NA> 5 Bossk 190 113 none green red 53 male Trandosha 6 Nute~ 191 90 none mottled g~ red NA male Cato Nei~ 7 BB8 NA NA none none black NA none <NA> # ... with 4 more variables: species , films , vehicles , # starships
ဒေတာအတွဲရှိ အတန်း ၇ တန်းသည် ဤအခြေအနေနှင့် ကိုက်ညီကြောင်း ကျွန်ုပ်တို့တွေ့မြင်နိုင်ပါသည်။
ဥပမာ 4- စာရင်းတစ်ခုရှိ တန်ဖိုးများနှင့်အတူ အတန်းများကို စစ်ထုတ်ပါ။
အရောင်စာရင်းတွင် မျက်လုံးအရောင် ပေါ်လာသည့် အတန်းများကိုလည်း စစ်ထုတ်နိုင်သည်-
starwars %>% filter(eye_color %in% c(' blue ', ' yellow ', ' red ')) # A tibble: 35 x 13 name height mass hair_color skin_color eye_color birth_year gender 1 Luke~ 172 77 blond fair blue 19 male 2 C-3PO 167 75 <NA> gold yellow 112 <NA> 3 R2-D2 96 32 <NA> white, bl~ red 33 <NA> 4 Dart~ 202 136 none white yellow 41.9 male 5 Owen~ 178 120 brown, gr~ light blue 52 male 6 Beru~ 165 75 brown light blue 47 female 7 R5-D4 97 32 <NA> white, red red NA <NA> 8 Anak~ 188 84 blond fair blue 41.9 male 9 Wilh~ 180 NA auburn, g~ fair blue 64 male 10 Chew~ 228 112 brown unknown blue 200 male # ... with 25 more rows, and 5 more variables: homeworld, species, # films, vehicles, starships
ဒေတာအတွဲရှိ 35 တန်းတွင် အပြာ၊ အဝါ သို့မဟုတ် မျက်လုံးနီအရောင်ရှိနေသည်ကို ကျွန်ုပ်တို့တွေ့မြင်နိုင်ပါသည်။
ဆက်စပ်- R ရှိ %in% အော်ပရေတာအား အသုံးပြုနည်း (ဥပမာများနှင့်အတူ)
ဥပမာ 5- ထက်နည်းသော သို့မဟုတ် ပိုကြီးသော အတန်းများကို စစ်ထုတ်ပါ။
ကိန်းဂဏာန်းကိန်းရှင်များတွင် လုပ်ဆောင်ချက်များထက် နည်းသော သို့မဟုတ် ကြီးသည်ထက်နည်းသော အတန်းများကိုလည်း စစ်ထုတ်နိုင်သည်-
#find rows where height is greater than 250 starwars %>% filter(height > 250) # A tibble: 1 x 13 name height mass hair_color skin_color eye_color birth_year gender homeworld 1 Yara~ 264 NA none white yellow NA male Quermia # ... with 4 more variables: species , films , vehicles , # starships #find rows where height is between 200 and 220 starwars %>% filter(height > 200 & height < 220) # A tibble: 5 x 13 name height mass hair_color skin_color eye_color birth_year gender homeworld 1 Dart~ 202 136 none white yellow 41.9 male Tatooine 2 Rugo~ 206 NA none green orange NA male Naboo 3 Taun~ 213 NA none gray black NA female Kamino 4 Grie~ 216 159 none brown, wh~ green, y~ NA male Kalee 5 Tion~ 206 80 none gray black NA male Utapau # ... with 4 more variables: species , films , vehicles , # starships #find rows where height is above the average height starwars %>% filter(height > mean (height, na.rm = TRUE )) # A tibble: 51 x 13 name height mass hair_color skin_color eye_color birth_year gender 1 Dart~ 202 136 none white yellow 41.9 male 2 Owen~ 178 120 brown, gr~ light blue 52 male 3 Bigg~ 183 84 black light brown 24 male 4 Obi-~ 182 77 auburn, w~ fair blue-gray 57 male 5 Anak~ 188 84 blond fair blue 41.9 male 6 Wilh~ 180 NA auburn, g~ fair blue 64 male 7 Chew~ 228 112 brown unknown blue 200 male 8 Han ~ 180 80 brown fair brown 29 male 9 Jabb~ 175 1358 <NA> green-tan~ orange 600 herma~ 10 Jek ~ 180 110 brown fair blue NA male # ... with 41 more rows, and 5 more variables: homeworld, species, # films, vehicles, starships
filter() လုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာဖွေနိုင်ပါသည်။