根据列的唯一级别修改数据框,然后将其中的2个其他列的值合并。

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

Modify dataframe based on the unique levels of column and then combine values from 2 other columns inside them

问题

我有以下数据框,我想以一种方式修改它,使其具有3列Election,Party_1和Party_2,在Party列内部粘贴CandidateVoteshare之间的内容

  1. df <- structure(list(Election = c("PC_2009", "AC_2011", "PC_2014",
  2. "AC_2016", "PC_2019", "AC_2021", "PC_2009", "AC_2011", "PC_2014",
  3. "AC_2016", "PC_2019", "AC_2021"), Party = c("Party_1", "Party_1",
  4. "Party_1", "Party_1", "Party_1", "Party_1", "Party_2", "Party_2",
  5. "Party_2", "Party_2", "Party_2", "Party_2"), Candidate = c("Mr.Sen",
  6. "Dr.Kar", "Mr. Sen", "Dr.Kar", "Dr.Reddy", "Dr.Kar", "Dr.Dutta",
  7. "Mrs.Dondopani", "Mr. Das", "Mrs.Dondopani", "Mr,Das", "Mrs.Dondopani"
  8. ), Voteshare = c(0.2, 0.32, 0.12, 0.36, 0.005, 0.26, 0.15, 0.23,
  9. 0.45, 0.28, 0.54, 0.38)), row.names = c(NA, -12L), class = c("tbl_df",
  10. "tbl", "data.frame"))

像这样:

根据列的唯一级别修改数据框,然后将其中的2个其他列的值合并。

英文:

I have the dataframe below and I want to modify it in a way that will have 3 columns Election,Party_1 and Party_2 and inside Party columns a paste between Candidate and Voteshare

  1. df&lt;-structure(list(ELECtion = c(&quot;PC_2009&quot;, &quot;AC_2011&quot;, &quot;PC_2014&quot;,
  2. &quot;AC_2016&quot;, &quot;PC_2019&quot;, &quot;AC_2021&quot;, &quot;PC_2009&quot;, &quot;AC_2011&quot;, &quot;PC_2014&quot;,
  3. &quot;AC_2016&quot;, &quot;PC_2019&quot;, &quot;AC_2021&quot;), Party = c(&quot;Party_1&quot;, &quot;Party_1&quot;,
  4. &quot;Party_1&quot;, &quot;Party_1&quot;, &quot;Party_1&quot;, &quot;Party_1&quot;, &quot;Party_2&quot;, &quot;Party_2&quot;,
  5. &quot;Party_2&quot;, &quot;Party_2&quot;, &quot;Party_2&quot;, &quot;Party_2&quot;), Candidate = c(&quot;Mr.Sen&quot;,
  6. &quot;Dr.Kar&quot;, &quot;Mr. Sen&quot;, &quot;Dr.Kar&quot;, &quot;Dr.Reddy&quot;, &quot;Dr.Kar&quot;, &quot;Dr.Dutta&quot;,
  7. &quot;Mrs.Dondopani&quot;, &quot;Mr. Das&quot;, &quot;Mrs.Dondopani&quot;, &quot;Mr,Das&quot;, &quot;Mrs.Dondopani&quot;
  8. ), Voteshare = c(0.2, 0.32, 0.12, 0.36, 0.005, 0.26, 0.15, 0.23,
  9. 0.45, 0.28, 0.54, 0.38)), row.names = c(NA, -12L), class = c(&quot;tbl_df&quot;,
  10. &quot;tbl&quot;, &quot;data.frame&quot;))

like

根据列的唯一级别修改数据框,然后将其中的2个其他列的值合并。

答案1

得分: 1

使用 paste + pivot_wider

  1. library(tidyr)
  2. library(dplyr)
  3. df %>%
  4. mutate(cand_vote = paste(Candidate, paste0(Voteshare * 100, "%")), .keep = "unused") %>%
  5. pivot_wider(names_from = "Party", values_from = "cand_vote")
  6. # ELECtion Party_1 Party_2
  7. # <chr> <chr> <chr>
  8. # 1 PC_2009 Mr.Sen 20% Dr.Dutta 15%
  9. # 2 AC_2011 Dr.Kar 32% Mrs.Dondopani 23%
  10. # 3 PC_2014 Mr. Sen 12% Mr. Das 45%
  11. # 4 AC_2016 Dr.Kar 36% Mrs.Dondopani 28%
  12. # 5 PC_2019 Dr.Reddy 0.5% Mr,Das 54%
  13. # 6 AC_2021 Dr.Kar 26% Mrs.Dondopani 38%
英文:

With paste + pivot_wider:

  1. library(tidyr)
  2. library(dplyr)
  3. df %&gt;%
  4. mutate(cand_vote = paste(Candidate, paste0(Voteshare * 100, &quot;%&quot;)), .keep = &quot;unused&quot;) %&gt;%
  5. pivot_wider(names_from = &quot;Party&quot;, values_from = &quot;cand_vote&quot;)
  6. # ELECtion Party_1 Party_2
  7. # &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  8. # 1 PC_2009 Mr.Sen 20% Dr.Dutta 15%
  9. # 2 AC_2011 Dr.Kar 32% Mrs.Dondopani 23%
  10. # 3 PC_2014 Mr. Sen 12% Mr. Das 45%
  11. # 4 AC_2016 Dr.Kar 36% Mrs.Dondopani 28%
  12. # 5 PC_2019 Dr.Reddy 0.5% Mr,Das 54%
  13. # 6 AC_2021 Dr.Kar 26% Mrs.Dondopani 38%

答案2

得分: 1

我们可以将候选人和选票份额列合并为一个单一变量,然后使用pivot_wider来展开数据。
scales::percent可以轻松将分数转换为百分比。

  1. library(tidyr)
  2. library(dplyr)
  3. df |&gt;
  4. mutate(Voteshare = scales::percent(Voteshare)) |&gt;
  5. unite(c(&quot;Candidate&quot;, &quot;Voteshare&quot;),
  6. sep = &quot; &quot;,
  7. col = &quot;value&quot;) |&gt;
  8. pivot_wider(names_from = Party,
  9. values_from = value)
  10. # 生成的数据表如下:
  11. ELECtion Party_1 Party_2
  12. 1 PC_2009 Mr.Sen 20.0% Dr.Dutta 15.0%
  13. 2 AC_2011 Dr.Kar 32.0% Mrs.Dondopani 23.0%
  14. 3 PC_2014 Mr. Sen 12.0% Mr. Das 45.0%
  15. 4 AC_2016 Dr.Kar 36.0% Mrs.Dondopani 28.0%
  16. 5 PC_2019 Dr.Reddy 0.5% Mr,Das 54.0%
  17. 6 AC_2021 Dr.Kar 26.0% Mrs.Dondopani 38.0%
英文:

We can unite the candidate and voteshare columns into a single variable, then pivot_wider to spread the data.
scales::percent converts fractions to percentages easily.

  1. library(tidyr)
  2. library(dplyr)
  3. df |&gt;
  4. mutate(Voteshare = scales::percent(Voteshare)) |&gt;
  5. unite(c(&quot;Candidate&quot;, &quot;Voteshare&quot;),
  6. sep = &quot; &quot;,
  7. col = &quot;value&quot;) |&gt;
  8. pivot_wider(names_from = Party,
  9. values_from = value)
  10. # A tibble: 6 &#215; 3
  11. ELECtion Party_1 Party_2
  12. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  13. 1 PC_2009 Mr.Sen 20.0% Dr.Dutta 15.0%
  14. 2 AC_2011 Dr.Kar 32.0% Mrs.Dondopani 23.0%
  15. 3 PC_2014 Mr. Sen 12.0% Mr. Das 45.0%
  16. 4 AC_2016 Dr.Kar 36.0% Mrs.Dondopani 28.0%
  17. 5 PC_2019 Dr.Reddy 0.5% Mr,Das 54.0%
  18. 6 AC_2021 Dr.Kar 26.0% Mrs.Dondopani 38.0%
  19. </details>

huangapple
  • 本文由 发表于 2023年6月12日 20:05:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456506.html
匿名

发表评论

匿名网友

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

确定