r - 过滤(dplyr)因子级别不会过滤它应该过滤的所有行
问题描述
我有一个政党数据集。我想将其中一些的电子结果绘制为 line_geom。我只想展示一些派对。所以我正在过滤我想要的各方的因素水平。从下面的 table() 可以看出,例如,对于党的 PSOE,我有每个选举年的值,但这些值没有被正确过滤掉(例如 1993、1996、2004),也没有在之后绘制。我不明白为什么会这样。
我认为这可能与一些政党多年来没有参加选举有关,所以我只是为PSOE和PP策划。但事实上,我想过滤并在主题标签之后的相应过滤行上显示这些各方。
请帮忙:
trial <- structure(list(year = c(1977L, 1977L, 1977L, 1977L, 1977L, 1977L,
1977L, 1977L, 1977L, 1977L, 1977L, 1977L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1989L, 1989L, 1989L,
1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L,
1989L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L,
1993L, 1993L, 1993L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L,
1996L, 1996L, 1996L, 1996L, 1996L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2004L,
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L,
2004L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
2008L, 2008L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L), partido = structure(c(30L, 28L, 23L, 2L, 29L, 10L, 24L,
31L, 15L, 14L, 5L, 19L, 30L, 28L, 23L, 2L, 10L, 32L, 27L, 24L,
17L, 15L, 14L, 4L, 22L, 33L, 28L, 2L, 30L, 23L, 10L, 7L, 24L,
17L, 15L, 14L, 28L, 2L, 7L, 10L, 23L, 24L, 17L, 14L, 8L, 22L,
6L, 35L, 28L, 2L, 23L, 7L, 10L, 24L, 17L, 21L, 35L, 13L, 14L,
22L, 6L, 28L, 2L, 23L, 10L, 24L, 6L, 17L, 15L, 22L, 13L, 35L,
2L, 28L, 23L, 10L, 24L, 6L, 3L, 17L, 15L, 13L, 35L, 2L, 28L,
23L, 10L, 24L, 3L, 6L, 21L, 15L, 18L, 13L, 9L, 28L, 2L, 23L,
10L, 15L, 24L, 6L, 3L, 9L, 13L, 20L, 28L, 2L, 23L, 10L, 24L,
34L, 15L, 3L, 6L, 20L, 2L, 28L, 23L, 34L, 10L, 1L, 24L, 15L,
3L, 6L, 12L, 16L, 20L, 2L, 28L, 11L, 25L, 25L, 23L, 25L, 15L,
10L, 25L, 24L, 17L, 6L, 2L, 28L, 25L, 11L, 25L, 25L, 15L, 10L,
25L, 24L, 17L, 6L, 28L, 2L, 11L, 25L, 36L, 15L, 25L, 10L, 24L,
17L, 25L, 12L, 6L, 37L, 26L), .Label = c("AMAIUR", "AP/PP", "BNG",
"C-UPC", "CAIC", "CC", "CDS", "CG", "CHA", "CIU/PDECAT", "CIUDADANOS",
"COMPROMIS", "EA", "EE", "ERC", "FAC", "HB/EHBILDU", "IC-V",
"INDEP", "NABAI/GBAI", "PA", "PAR", "PCE/IU", "PNV", "PODEMOS",
"PRC", "PSA-PA", "PSOE", "PSP-US", "UCD", "UDC-IDCC", "UN", "UPN",
"UPYD", "UV", "VOX", "NA+"), class = "factor"), diputadosPerc = c(47.1428571428571,
33.7142857142857, 5.71428571428571, 4.57142857142857, 1.71428571428571,
3.14285714285714, 2.28571428571429, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 48,
34.5714285714286, 6.57142857142857, 2.57142857142857, 2.28571428571429,
0.285714285714286, 1.42857142857143, 2, 0.857142857142857, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
57.7142857142857, 30.5714285714286, 3.14285714285714, 1.14285714285714,
3.42857142857143, 0.571428571428571, 2.28571428571429, 0.571428571428571,
0.285714285714286, 0.285714285714286, 52.5714285714286, 30, 5.42857142857143,
5.14285714285714, 2, 1.71428571428571, 1.42857142857143, 0.571428571428571,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
50, 30.5714285714286, 4.85714285714286, 4, 5.14285714285714,
1.42857142857143, 1.14285714285714, 0.571428571428571, 0.571428571428571,
0.571428571428571, 0.571428571428571, 0.285714285714286, 0.285714285714286,
45.4285714285714, 40.2857142857143, 5.14285714285714, 4.85714285714286,
1.42857142857143, 1.14285714285714, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 44.5714285714286,
40.2857142857143, 6, 4.57142857142857, 1.42857142857143, 1.14285714285714,
0.571428571428571, 0.571428571428571, 0.285714285714286, 0.285714285714286,
0.285714285714286, 52.2857142857143, 35.7142857142857, 2.28571428571429,
4.28571428571429, 2, 0.857142857142857, 1.14285714285714, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
46.8571428571429, 42.2857142857143, 1.42857142857143, 2.85714285714286,
2.28571428571429, 2, 0.857142857142857, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 48.2857142857143, 44, 0.571428571428571,
2.85714285714286, 1.71428571428571, 0.285714285714286, 0.857142857142857,
0.571428571428571, 0.571428571428571, 0.285714285714286, 53.1428571428571,
31.4285714285714, 3.14285714285714, 1.42857142857143, 4.57142857142857,
2, 1.42857142857143, 0.857142857142857, 0.571428571428571, 0.571428571428571,
0.285714285714286, 0.285714285714286, 0.285714285714286, 35.1428571428571,
25.7142857142857, 11.4285714285714, 12, 3.42857142857143, 0.571428571428571,
2.57142857142857, 2.57142857142857, 2.28571428571429, 1.71428571428571,
1.71428571428571, 0.571428571428571, 0.285714285714286, 39.1428571428571,
24.2857142857143, 12.8571428571429, 9.14285714285714, 3.42857142857143,
2.57142857142857, 2.57142857142857, 2.28571428571429, 1.42857142857143,
1.42857142857143, 0.571428571428571, 0.285714285714286, 35.1428571428571,
18.8571428571429, 16.2857142857143, 9.42857142857143, 6.85714285714286,
4.28571428571429, 2, 2, 1.71428571428571, 1.14285714285714, 0.571428571428571,
0.285714285714286, 0.571428571428571, 0.571428571428571, 0.285714285714286
), diputadosAbs = c(165, 118, 20, 16, 6, 11, 8, 2, 1, 1, 1, 1,
168, 121, 23, 9, 8, 1, 5, 7, 3, 1, 1, 1, 1, 1, 202, 107, 11,
4, 12, 2, 8, 2, 1, 1, 184, 105, 19, 18, 7, 6, 5, 2, 1, 1, 1,
1, 175, 107, 17, 14, 18, 5, 4, 2, 2, 2, 2, 1, 1, 159, 141, 18,
17, 5, 4, 2, 1, 1, 1, 1, 156, 141, 21, 16, 5, 4, 2, 2, 1, 1,
1, 183, 125, 8, 15, 7, 3, 4, 1, 1, 1, 1, 1, 164, 148, 5, 10,
8, 7, 3, 2, 1, 1, 1, 169, 154, 2, 10, 6, 1, 3, 2, 2, 1, 186,
110, 11, 5, 16, 7, 5, 3, 2, 2, 1, 1, 1, 123, 90, 40, 42, 12,
2, 9, 9, 8, 6, 6, 2, 1, 137, 85, 45, 32, 12, 9, 9, 8, 5, 5, 2,
1, 123, 66, 57, 33, 24, 15, 7, 7, 6, 4, 2, 1, 2, 2, 1)), row.names = c(NA,
-169L), class = "data.frame")
.
table(trial$partido, trial$year)
trial %>%
filter(partido == c("PSOE", "AP/PP")) %>% #, "CIU/PDECAT", "PNV","PCE/IU", "CDS", "UCD")) %>%
select(year, partido, diputadosPerc) %>%
# melt(natElecSeatsOnly, id = "year", measure.vars = names(natElecSeatsOnly)[c(2,14)], variable.name = "values") %>%
ggplot(aes(year, diputadosPerc, colour=partido)) + geom_line()
.
Session info: R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.4.3 bindrcpp_0.2.2 forcats_0.3.0 stringr_1.3.1 dplyr_0.7.8 purrr_0.2.5 readr_1.3.1
[8] tidyr_0.8.2 tibble_1.4.2 ggplot2_3.1.0 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 cellranger_1.1.0 pillar_1.3.0 compiler_3.5.2 plyr_1.8.4 bindr_0.1.1
[7] tools_3.5.2 digest_0.6.18 jsonlite_1.5 lubridate_1.7.4 nlme_3.1-137 gtable_0.2.0
[13] lattice_0.20-38 pkgconfig_2.0.2 rlang_0.3.0.1 cli_1.0.1 rstudioapi_0.8 yaml_2.2.0
[19] haven_2.1.0 withr_2.1.2 xml2_1.2.0 httr_1.3.1 knitr_1.20 hms_0.4.2
[25] grid_3.5.2 tidyselect_0.2.5 glue_1.3.0 R6_2.3.0 readxl_1.1.0 modelr_0.1.2
[31] magrittr_1.5 backports_1.1.2 scales_1.0.0 rvest_0.3.2 assertthat_0.2.0 colorspace_1.3-2
[37] labeling_0.3 stringi_1.2.4 lazyeval_0.2.1 munsell_0.5.0 broom_0.5.0 crayon_1.3.4
解决方案
%in%
像这样使用
trial %>%
filter(partido %in% c("PSOE", "AP/PP")) %>% #, "CIU/PDECAT", "PNV","PCE/IU", "CDS", "UCD")) %>%
select(year, partido, diputadosPerc) %>%
# melt(natElecSeatsOnly, id = "year", measure.vars = names(natElecSeatsOnly)[c(2,14)], variable.name = "values") %>%
ggplot(aes(year, diputadosPerc, colour=partido)) + geom_line()
推荐阅读
- amcharts4 - Amchart 标签和标题位置不正确
- javascript - 如何在 SAP 混合应用程序中包含具有相对路径的集合?
- elm - 如何从 elm 对象中获取特定的键值对
- python - 如何在一个请求中收集所有可用的 Pod 指标
- sql - SQL如何查询总计和小计
- regex - 我的公式只保留一个单词,空格后没有任何内容
- javascript - 将每个对象属性乘以 2 并从结果中创建新对象
- javascript - 使用一个链接滚动两列
- linux - 从 LINUX 特定目录下的文件中查找特定字符串
- flutter - 如何更改 Flutter TextButton 的高度?