group_by&总结5个数据范围中的4个工作
我正在应用以下功能来为每个选区找到最受欢迎的方:
election <- elec_df %>% dplyr::filter(Election == 2017 & WKR_NR <= 299) #%>%
dplyr::group_by(WKR_NR, Partei) %>%
summarise(
Anteil_Stimmen = Stimmen/Total_Erststimmen,
Max_Partei = max(Anteil_Stimmen, na.rm=TRUE)) %>%
dplyr::filter(Max_Partei == max(Max_Partei, na.rm=TRUE))
该代码在过滤几年时效果很好:2005,2009,2013,但未能分组并总结2017年
因此,我认为问题必须与2017年独有的政党变量有关。但是,我找不到错误。
可以找到数据集
?高度赞赏。谢谢你:)
I am applying the following function to find the most popular party for each constituency:
election <- elec_df %>% dplyr::filter(Election == 2017 & WKR_NR <= 299) #%>%
dplyr::group_by(WKR_NR, Partei) %>%
summarise(
Anteil_Stimmen = Stimmen/Total_Erststimmen,
Max_Partei = max(Anteil_Stimmen, na.rm=TRUE)) %>%
dplyr::filter(Max_Partei == max(Max_Partei, na.rm=TRUE))
The code works fine when filtering for the years: 2005, 2009, 2013, but fails to group and summarize for the year 2017
I thus assume that the problem must be related to the Party variable that is exclusive to the year 2017. However, I cannot find the mistake.
The data set can be found here
Any kind of hint is highly appreciated. Thank you:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新7/11/22:
如果从提供的链接中下载了原始数据,请阅读(即使用
read.csv
,readr :: Read_csv
,或<代码> data.table :: fread ),并分配给df
,然后可以使用以下整洁管道来获得每次选举的299行:如果您想通过选举拆分(IE)到年度)您可以将其添加到管道的末尾:
输出:
Update 7/11/22:
If the raw data are downloaded from the link as provided, read into R (i.e. using
read.csv
,readr::read_csv
, ordata.table::fread
), and assigned todf
, then one can use the following tidy pipeline to get 299 rows per Election:If you want to split by Election (i.e. by Year) you can add this to the end of the pipeline:
Output: