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

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

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之间的内容

df <- structure(list(Election = c("PC_2009", "AC_2011", "PC_2014", 
"AC_2016", "PC_2019", "AC_2021", "PC_2009", "AC_2011", "PC_2014", 
"AC_2016", "PC_2019", "AC_2021"), Party = c("Party_1", "Party_1", 
"Party_1", "Party_1", "Party_1", "Party_1", "Party_2", "Party_2", 
"Party_2", "Party_2", "Party_2", "Party_2"), Candidate = c("Mr.Sen", 
"Dr.Kar", "Mr. Sen", "Dr.Kar", "Dr.Reddy", "Dr.Kar", "Dr.Dutta", 
"Mrs.Dondopani", "Mr. Das", "Mrs.Dondopani", "Mr,Das", "Mrs.Dondopani"
), Voteshare = c(0.2, 0.32, 0.12, 0.36, 0.005, 0.26, 0.15, 0.23, 
0.45, 0.28, 0.54, 0.38)), row.names = c(NA, -12L), class = c("tbl_df", 
"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

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

like

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

答案1

得分: 1

使用 paste + pivot_wider

library(tidyr)
library(dplyr)
df %>% 
  mutate(cand_vote = paste(Candidate, paste0(Voteshare * 100, "%")), .keep = "unused") %>% 
  pivot_wider(names_from = "Party", values_from = "cand_vote")

#   ELECtion Party_1       Party_2          
#   <chr>    <chr>         <chr>            
# 1 PC_2009  Mr.Sen 20%    Dr.Dutta 15%     
# 2 AC_2011  Dr.Kar 32%    Mrs.Dondopani 23%
# 3 PC_2014  Mr. Sen 12%   Mr. Das 45%      
# 4 AC_2016  Dr.Kar 36%    Mrs.Dondopani 28%
# 5 PC_2019  Dr.Reddy 0.5% Mr,Das 54%       
# 6 AC_2021  Dr.Kar 26%    Mrs.Dondopani 38%
英文:

With paste + pivot_wider:

library(tidyr)
library(dplyr)
df %&gt;% 
  mutate(cand_vote = paste(Candidate, paste0(Voteshare * 100, &quot;%&quot;)), .keep = &quot;unused&quot;) %&gt;% 
  pivot_wider(names_from = &quot;Party&quot;, values_from = &quot;cand_vote&quot;)

#   ELECtion Party_1       Party_2          
#   &lt;chr&gt;    &lt;chr&gt;         &lt;chr&gt;            
# 1 PC_2009  Mr.Sen 20%    Dr.Dutta 15%     
# 2 AC_2011  Dr.Kar 32%    Mrs.Dondopani 23%
# 3 PC_2014  Mr. Sen 12%   Mr. Das 45%      
# 4 AC_2016  Dr.Kar 36%    Mrs.Dondopani 28%
# 5 PC_2019  Dr.Reddy 0.5% Mr,Das 54%       
# 6 AC_2021  Dr.Kar 26%    Mrs.Dondopani 38%

答案2

得分: 1

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

library(tidyr)
library(dplyr)

df |&gt; 
    mutate(Voteshare = scales::percent(Voteshare)) |&gt; 
    unite(c(&quot;Candidate&quot;, &quot;Voteshare&quot;),
          sep = &quot; &quot;,
          col = &quot;value&quot;) |&gt; 
    pivot_wider(names_from = Party,
                values_from = value)

# 生成的数据表如下:
  ELECtion Party_1       Party_2            
1 PC_2009  Mr.Sen 20.0%  Dr.Dutta 15.0%     
2 AC_2011  Dr.Kar 32.0%  Mrs.Dondopani 23.0%
3 PC_2014  Mr. Sen 12.0% Mr. Das 45.0%      
4 AC_2016  Dr.Kar 36.0%  Mrs.Dondopani 28.0%
5 PC_2019  Dr.Reddy 0.5% Mr,Das 54.0%       
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.

library(tidyr)
library(dplyr)

df |&gt; 
    mutate(Voteshare = scales::percent(Voteshare)) |&gt; 
    unite(c(&quot;Candidate&quot;, &quot;Voteshare&quot;),
          sep = &quot; &quot;,
          col = &quot;value&quot;) |&gt; 
    pivot_wider(names_from = Party,
                values_from = value)

# A tibble: 6 &#215; 3
  ELECtion Party_1       Party_2            
  &lt;chr&gt;    &lt;chr&gt;         &lt;chr&gt;              
1 PC_2009  Mr.Sen 20.0%  Dr.Dutta 15.0%     
2 AC_2011  Dr.Kar 32.0%  Mrs.Dondopani 23.0%
3 PC_2014  Mr. Sen 12.0% Mr. Das 45.0%      
4 AC_2016  Dr.Kar 36.0%  Mrs.Dondopani 28.0%
5 PC_2019  Dr.Reddy 0.5% Mr,Das 54.0%       
6 AC_2021  Dr.Kar 26.0%  Mrs.Dondopani 38.0%

</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:

确定