การยุบคอลัมน์ในเวอร์ชัน R Easy

ฉันรู้ว่ามีคำถามบางอย่างที่คล้ายกับคำถามนี้อยู่แล้ว แต่ฉันเป็นมือใหม่ในด้าน r และฉันแค่ไม่เข้าใจคำตอบสำหรับคำถามเหล่านั้น! หากคุณสามารถให้คำตอบที่เรียบง่ายไม่ซับซ้อนสำหรับคำถามของฉันได้ ฉันจะขอบคุณมาก!!

ฉันกำลังพยายามยุบหมวดหมู่ในคอลัมน์ในกรอบข้อมูลของฉัน ซึ่งสรุปจะมีลักษณะดังนี้ (หมวดหมู่ = n):

1-Self = 62
2-Mother = 0
3-Father = 43
4-Grandfather = 142
5-Grandmother = 17
6-Uncle = 1
7-Brother = 2
8-Husband = 5
9-Aunt = 0
10-Sister = 2
11-CommunityInfluencer = 1
12-ReligiousFigure = 0
13-Other = 4
14-N/A = 9

ฉันต้องการรวมหมวดหมู่ 6-14 แต่ทั้งหมดอยู่ในคอลัมน์เดียวกัน จะต้องทำอย่างไร หากคุณสามารถระบุโครงสร้างสำหรับวิธีเขียนโค้ดนี้ได้ ฉันจะขอบคุณมาก!

ขอบคุณ!


person Cath    schedule 16.09.2020    source แหล่งที่มา
comment
สวัสดี Cath และยินดีต้อนรับ ฉันแน่ใจว่ามีคนยินดีที่จะช่วยเหลือ แต่เพื่อให้สามารถทำเช่นนั้นได้ เราจำเป็นต้องเข้าใจข้อมูลของคุณดีขึ้น โปรดช่วย dput(head(yourdataframe, 10))   -  person Chuck P    schedule 16.09.2020
comment
หากฉันเข้าใจคำถามของคุณถูกต้อง forcats::fct_lump อาจจะทำงานที่นี่ forcats.tidyverse.org/ การอ้างอิง/fct_lump.html   -  person NColl    schedule 16.09.2020
comment
@NColl สิ่งนี้มีประโยชน์มาก คุณช่วยแบ่งปันตัวอย่างการใช้ forcats ได้ไหม ฉันไม่ค่อยเข้าใจวิธีใช้   -  person Cath    schedule 16.09.2020
comment
@ChuckP ฉันได้รวมสิ่งนี้ไว้ใน dataframe ของฉันแล้ว ขอบคุณ!   -  person Cath    schedule 16.09.2020


คำตอบ (1)


เมื่อใช้ forcats::fct_other คุณสามารถระบุหมวดหมู่ที่คุณต้องการรวมเป็นหมวดหมู่อื่นๆ ได้

ตัวอย่างเช่น:

library(tidyverse)

drop_cats <- c("Naboo", "Stewjon")

starwars %>%
  mutate(homeworld =  fct_other(homeworld, drop = drop_cats)) 

A tibble: 87 x 13
   name       height  mass hair_color   skin_color eye_color birth_year gender homeworld species films  vehicles starships
   <chr>       <int> <dbl> <chr>        <chr>      <chr>          <dbl> <chr>  <fct>     <chr>   <list> <list>   <list>   
 1 Luke Skyw…    172    77 blond        fair       blue            19   male   Tatooine  Human   <chr … <chr [2… <chr [2]>
 2 C-3PO         167    75 NA           gold       yellow         112   NA     Tatooine  Droid   <chr … <chr [0… <chr [0]>
 3 R2-D2          96    32 NA           white, bl… red             33   NA     Other     Droid   <chr … <chr [0… <chr [0]>
 4 Darth Vad…    202   136 none         white      yellow          41.9 male   Tatooine  Human   <chr … <chr [0… <chr [1]>
 5 Leia Orga…    150    49 brown        light      brown           19   female Alderaan  Human   <chr … <chr [1… <chr [0]>
 6 Owen Lars     178   120 brown, grey  light      blue            52   male   Tatooine  Human   <chr … <chr [0… <chr [0]>
 7 Beru Whit…    165    75 brown        light      blue            47   female Tatooine  Human   <chr … <chr [0… <chr [0]>
 8 R5-D4          97    32 NA           white, red red             NA   NA     Tatooine  Droid   <chr … <chr [0… <chr [0]>
 9 Biggs Dar…    183    84 black        light      brown           24   male   Tatooine  Human   <chr … <chr [0… <chr [1]>
10 Obi-Wan K…    182    77 auburn, whi… fair       blue-gray       57   male   Other     Human   <chr … <chr [1… <chr [5]>
# … with 77 more rows
person NColl    schedule 16.09.2020