英文:
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列内部粘贴Candidate
和Voteshare
之间的内容
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"))
像这样:
英文:
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<-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"))
like
答案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 %>%
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%
答案2
得分: 1
我们可以将候选人和选票份额列合并为一个单一变量,然后使用pivot_wider
来展开数据。
scales::percent
可以轻松将分数转换为百分比。
library(tidyr)
library(dplyr)
df |>
mutate(Voteshare = scales::percent(Voteshare)) |>
unite(c("Candidate", "Voteshare"),
sep = " ",
col = "value") |>
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 |>
mutate(Voteshare = scales::percent(Voteshare)) |>
unite(c("Candidate", "Voteshare"),
sep = " ",
col = "value") |>
pivot_wider(names_from = Party,
values_from = value)
# A tibble: 6 × 3
ELECtion Party_1 Party_2
<chr> <chr> <chr>
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论