在R中,如何将多个变量分支到另一个变量下?

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

How could one branch multiple variables under another variable in R?

问题

我正在尝试按一组变量将我的观察结果分组,这些变量最终会归属于最后一组变量。这是我的示例数据:

  1. 国家 姓名 民族 党派
  2. 阿富汗 约翰 普什图人 X
  3. 阿富汗 奥利弗 普什图人 Y
  4. 阿富汗 布拉德 塔吉克人 X
  5. 阿富汗 查德 哈扎拉人 X
  6. 波斯尼亚 维尔京 塞尔维亚人 P
  7. 波斯尼亚 玛丽 塞尔维亚人 P
  8. 波斯尼亚 耶稣 克罗地亚人 C

我要做的是在每个党派下创建所有现有民族的集合,并计算在每个党派下有多少人属于每个民族,还要考虑国家,结果应该如下所示:

  1. 国家 党派 民族 人数
  2. 阿富汗 X 普什图人 1
  3. 阿富汗 X 塔吉克人 1
  4. 阿富汗 X 哈扎拉人 1
  5. 阿富汗 Y 普什图人 1
  6. 阿富汗 Y 塔吉克人 0
  7. 阿富汗 Y 哈扎拉人 0
  8. 波斯尼亚 P 塞尔维亚人 2
  9. 波斯尼亚 P 克罗地亚人 0
  10. 波斯尼亚 C 塞尔维亚人 0
  11. 波斯尼亚 C 克罗地亚人 1

到目前为止,我尝试了 group_byaggregate 函数,但没有成功。

英文:

I'm trying to group my observations by a set of variables under another set of variables which is, finally, under a last set of variables. Here's what I have for example:

  1. country name ethnicity party
  2. Afghanistan john Pashtun X Party
  3. Afghanistan oliver Pashtun Y Party
  4. Afghanistan brad Tajik X Party
  5. Afghanistan chad Hazara X Party
  6. Bosnia virgin Serb P Party
  7. Bosnia mary Serb P Party
  8. Bosnia jesus Croat C Party

What I'm going for should create the set of all existing ethnicities under each party and count how many persons are under each ethnicity in a party, within a country and look something like:

  1. country party ethnicity count
  2. Afghanistan X Party Pashtun 1
  3. Afghanistan X Party Tajik 1
  4. Afghanistan X Party Hazara 1
  5. Afghanistan Y Party Pashtun 1
  6. Afghanistan Y Party Tajik 0
  7. Afghanistan Y Party Hazara 0
  8. Bosnia P Party Serb 2
  9. Bosnia P Party Croat 0
  10. Bosnia C Party Serb 0
  11. Bosnia C Party Croat 1

So far I've tried the functions group_by and aggregate to no avail.

答案1

得分: 1

这是一个非常简单的操作,请阅读这本书 https://r4ds.had.co.nz/

  1. library(data.table)
  2. library(tidyverse)
  3. df_example <- fread("country name ethnicity party coolness
  4. Afghanistan john Pashtun X_Party cool
  5. Afghanistan oliver Pashtun Y_Party not_cool
  6. Afghanistan brad Tajik X_Party cool
  7. Afghanistan chad Hazara X_Party not_cool
  8. Bosnia virgin Serb P_Party cool
  9. Bosnia mary Serb P_Party cool
  10. Bosnia jesus Croat C_Party not_cool" ,
  11. header = TRUE)
  12. df_example %>%
  13. group_by(country,ethnicity,party) %>%
  14. add_tally() %>%
  15. select(-name) %>%
  16. distinct()
英文:

this is a really simply operation, please read this book https://r4ds.had.co.nz/

  1. library(data.table)
  2. library(tidyverse)
  3. df_example &lt;- fread(&quot;country name ethnicity party coolness
  4. Afghanistan john Pashtun X_Party cool
  5. Afghanistan oliver Pashtun Y_Party not_cool
  6. Afghanistan brad Tajik X_Party cool
  7. Afghanistan chad Hazara X_Party not_cool
  8. Bosnia virgin Serb P_Party cool
  9. Bosnia mary Serb P_Party cool
  10. Bosnia jesus Croat C_Party not_cool&quot; ,
  11. header = TRUE)
  12. df_example %&gt;%
  13. group_by(country,ethnicity,party) %&gt;%
  14. add_tally() %&gt;%
  15. select(-name) %&gt;% # Some stuff that you don&#39;t want
  16. distinct()

答案2

得分: 1

你可以使用 dplyrtidyr

  1. df %>%
  2. count(!!!select(., -name)) %>%
  3. group_by(country) %>%
  4. complete(ethnicity, nesting(party), fill = list(n = 0))
  5. country ethnicity party n
  6. <chr> <chr> <fct> <dbl>
  7. 1 Afghanistan Hazara X Party 1
  8. 2 Afghanistan Hazara Y Party 0
  9. 3 Afghanistan Pashtun X Party 1
  10. 4 Afghanistan Pashtun Y Party 1
  11. 5 Afghanistan Tajik X Party 1
  12. 6 Afghanistan Tajik Y Party 0
  13. 7 Bosnia Croat C Party 1
  14. 8 Bosnia Croat P Party 0
  15. 9 Bosnia Serb C Party 0
  16. 10 Bosnia Serb P Party 2
英文:

You can use dplyr and tidyr:

  1. df %&gt;%
  2. count(!!!select(., -name)) %&gt;%
  3. group_by(country) %&gt;%
  4. complete(ethnicity, nesting(party), fill = list(n = 0))
  5. country ethnicity party n
  6. &lt;chr&gt; &lt;chr&gt; &lt;fct&gt; &lt;dbl&gt;
  7. 1 Afghanistan Hazara X Party 1
  8. 2 Afghanistan Hazara Y Party 0
  9. 3 Afghanistan Pashtun X Party 1
  10. 4 Afghanistan Pashtun Y Party 1
  11. 5 Afghanistan Tajik X Party 1
  12. 6 Afghanistan Tajik Y Party 0
  13. 7 Bosnia Croat C Party 1
  14. 8 Bosnia Croat P Party 0
  15. 9 Bosnia Serb C Party 0
  16. 10 Bosnia Serb P Party 2

huangapple
  • 本文由 发表于 2020年1月3日 21:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579796.html
匿名

发表评论

匿名网友

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

确定