R တွင်ပွားနေသောအတန်းများကိုမည်သို့ဖယ်ရှားနည်း (ဥပမာများဖြင့်)


R ရှိ ဒေတာဘောင်တစ်ခုမှ ထပ်နေသောအတန်းများကို ဖယ်ရှားရန် နည်းလမ်းနှစ်ခုအနက်မှ တစ်ခုကို သင်အသုံးပြုနိုင်သည်-

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

 #remove duplicate rows across entire data frame
df[ ! duplicated(df), ]

#remove duplicate rows across specific columns of data frame
df[ ! duplicated(df[c(' var1 ')]), ]

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

 #remove duplicate rows across entire data frame 
df %>%
  distinct(.keep_all = TRUE )

#remove duplicate rows across specific columns of data frame
df %>%
  distinct(var1, .keep_all = TRUE )

အောက်ပါဥပမာများသည် အောက်ပါဒေတာဘောင်ဖြင့် ဤ syntax ကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ပြသသည်-

 #define data frame
df <- data. frame (team=c('A', 'A', 'A', 'B', 'B', 'B'),
                 position=c('Guard', 'Guard', 'Forward', 'Guard', 'Center', 'Center'))

#view data frame
df

  team position
1A Guard
2 A Guard
3 A Forward
4 B Guard
5B Center
6B Center

ဥပမာ 1- Base R ကိုသုံး၍ ထပ်နေသောအတန်းများကို ဖယ်ရှားပါ။

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

 #remove duplicate rows from data frame
df[ ! duplicated(df), ]

  team position
1A Guard
3 A Forward
4 B Guard
5B Center

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

 #remove rows where there are duplicates in the 'team' column
df[ ! duplicated(df[c(' team ')]), ]

  team position
1A Guard
4 B Guard

ဥပမာ 2- dplyr ကိုသုံး၍ ထပ်နေသောအတန်းများကို ဖယ်ရှားပါ။

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

 library (dplyr)

#remove duplicate rows from data frame
df %>%
  distinct(.keep_all = TRUE )

  team position
1A Guard
2 A Forward
3 B Guard
4B Center

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

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

 library (dplyr)

#remove duplicate rows from data frame
df %>%
  distinct(team, .keep_all = TRUE )

  team position
1A Guard
2 B Guard

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

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

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

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

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