英文:
I Want to Group IDs According to Eco_Status Using R
问题
我想要另一个数据框,其中包含四个Eco_Status作为列,并且将每个ID按照它们在每个Eco_Status下出现的情况分组。
给定上述数据框df,我想要一个包含poor、average、rich和billionaire作为它们状态的ID的向量。
英文:
I want another data frame that has the four(4) Eco_Status as column and group each ID as each occurs under each Eco_Status
# group ID_numbers according to their respective status
df <- read.table(text =
                   "ID Pof_Exp  Eco_Status Gender
              31304  2  poor male
              31310  4  poor female
              31307  6  rich male
              31302  8  average male
              31301 10 billionaire male
              31308  2  poor female
              31316  4  poor male
              31317  6  rich male
              31312  8  average female
              31306 10 average female
              31314  2  poor female
              31311  4  average male
              31305  6  rich male
              31303  8  average male
              31309 10  average female
              31324  2  poor male
              31320  4  poor female
              31327  6  average male
              31322  8  average female
              31321 10 billionaire    male",
              header = TRUE)
Given the above data frame df, I want a vector of ID having poor, average, rich, and billionaire as their status.
答案1
得分: 2
这是另一个可能的解决方案:
df %>%
  select(-c(Pof_Exp, Gender)) %>%
  group_by(Eco_Status) %>%
  mutate(row = row_number()) %>%
  tidyr::pivot_wider(names_from = Eco_Status, values_from = ID) %>%
  select(-row)
这将产生以下结果:
   poor  rich average billionaire
  <int> <int>   <int>       <int>
1 31304 31307   31302       31301
2 31310 31317   31312       31321
3 31308 31305   31306          NA
4 31316    NA   31311          NA
5 31314    NA   31303          NA
6 31324    NA   31309          NA
7 31320    NA   31327          NA
8    NA    NA   31322          NA
英文:
here is another possible solution:
df %>% select(-c(Pof_Exp, Gender)) %>% group_by(Eco_Status) %>% 
  mutate(row = row_number()) %>%tidyr::pivot_wider(names_from = Eco_Status, values_from = ID) %>% 
  select(-row)
which gives:
poor  rich average billionaire
  <int> <int>   <int>       <int>
1 31304 31307   31302       31301
2 31310 31317   31312       31321
3 31308 31305   31306          NA
4 31316    NA   31311          NA
5 31314    NA   31303          NA
6 31324    NA   31309          NA
7 31320    NA   31327          NA
8    NA    NA   31322          NA
答案2
得分: 1
像这样吗?
df %>%
    group_by(Eco_Status) %>%
    summarise(ID = list(ID))
Eco_Status                                                     ID
1     average 31302, 31312, 31306, 31311, 31303, 31309, 31327, 31322
2 billionaire                                           31301, 31321
3        poor        31304, 31310, 31308, 31316, 31314, 31324, 31320
4        rich                                    31307, 31317, 31305
英文:
Something like this?
df %>%
    group_by(Eco_Status) %>%
    summarise(ID = list(ID))
   Eco_Status                                                     ID
1     average 31302, 31312, 31306, 31311, 31303, 31309, 31327, 31322
2 billionaire                                           31301, 31321
3        poor        31304, 31310, 31308, 31316, 31314, 31324, 31320
4        rich                                    31307, 31317, 31305
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论