我想使用R根据Eco_Status分组ID。

huangapple go评论66阅读模式
英文:

I Want to Group IDs According to Eco_Status Using R

问题

我想要另一个数据框,其中包含四个Eco_Status作为列,并且将每个ID按照它们在每个Eco_Status下出现的情况分组。

给定上述数据框df,我想要一个包含pooraveragerichbillionaire作为它们状态的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 %&gt;% select(-c(Pof_Exp, Gender)) %&gt;% group_by(Eco_Status) %&gt;% 
  mutate(row = row_number()) %&gt;%tidyr::pivot_wider(names_from = Eco_Status, values_from = ID) %&gt;% 
  select(-row)

which gives:

poor  rich average billionaire
  &lt;int&gt; &lt;int&gt;   &lt;int&gt;       &lt;int&gt;
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 %&gt;%
    group_by(Eco_Status) %&gt;%
    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

huangapple
  • 本文由 发表于 2023年7月12日 21:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定