没有适用于类对象的“重命名”方法 - 在 R 中重命名列

No applicable method for 'rename' applied to an object of class - renaming column in R

提问人:Viktor 提问时间:10/26/2023 最后编辑:Viktor 更新时间:10/26/2023 访问量:64

问:

我尝试重命名数据框中 4 个选定列中的 2 个,如下所示:wybory_2005

library(dplyr)

wybory_2005 <- wybory_2005_raw %>%
  select('Powiat', 'Rok', '6 - Prawo i Sprawiedliwość', '8 - Platforma Obywatelska') %>%
  rename('Prawo i Sprawiedliwość' = '6 - Prawo i Sprawiedliwość','Platforma Obywatelska' = '8 - Platforma Obywatelska')

但是我收到这样的错误:

没有适用于类对象的“重命名”方法 “字符”

编辑:我添加了下面要求的命令:

>dput(head(wybory_2005_raw))
structure(list(`Nr
okregu` = c(1, 1, 1, 1, 1, 1), TERYT = c("020100", 
"020300", "020500", "020600", "020700", "020900"), Powiat = c("boleslawiecki", 
"glogowski", "jaworski", "jeleniogórski", "kamiennogórski", "legnicki"
), `Uprawnieni do glosowania` = c(70154, 71771, 42639, 52884, 
37966, 41918), `Karty wydane (frekwencja)` = c(24559, 27600, 
15079, 18358, 12644, 14565), `Karty wazne (glosy oddane)` = c(24551, 
27582, 15040, 18348, 12642, 14536), `Glosy niewazne` = c(1123, 
1163, 621, 969, 711, 618), `Glosy wazne` = c(23428, 26419, 14419, 
17379, 11931, 13918), `1 - Ruch Patriotyczny` = c(389, 128, 173, 
231, 132, 110), `2 - Polska Partia Pracy` = c(181, 189, 90, 148, 
107, 110), `3 - Liga Polskich Rodzin` = c(1774, 1588, 1091, 1155, 
829, 1119), `4 - Partia Demokratyczna` = c(2137, 1834, 189, 429, 
862, 338), `5 - Socjaldemokracja Polska` = c(585, 965, 443, 1124, 
301, 277), `6 - Prawo i Sprawiedliwosc` = c(4985, 7899, 4494, 
3620, 3473, 2730), `7 - Sojusz Lewicy Demokratycznej` = c(3308, 
3720, 1735, 2467, 1830, 1663), `8 - Platforma Obywatelska` = c(5910, 
5562, 2603, 5129, 2202, 2279), `9 - Polska Partia Narodowa` = c(48, 
109, 41, 57, 37, 48), `10 - Polskie Stronnictwo Ludowe` = c(708, 
699, 913, 558, 406, 1641), `11 - Centrum` = c(66, 104, 125, 46, 
42, 45), `12 - Platforma Janusza Korwin-Mikke` = c(466, 411, 
186, 443, 135, 154), `13 - Ogólnopolska Koalicja Obywatelska O.K.O.` = c(0, 
0, 0, 0, 0, 0), `14 - Polska Konfederacja - Godnosc i Praca` = c(0, 
0, 0, 0, 0, 0), `15 - Samoobrona RP` = c(2562, 3090, 2244, 1856, 
1514, 3318), `16 - Inicjatywa RP` = c(0, 0, 0, 0, 0, 0), `17 - Dom Ojczysty` = c(267, 
83, 61, 78, 35, 51), `18 - Narodowe Odrodzenie Polski` = c(42, 
38, 31, 38, 26, 35), `19 - Stronnictwo Pracy` = c(0, 0, 0, 0, 
0, 0), `19  - Spoleczni Ratownicy` = c(0, 0, 0, 0, 0, 0), `19  - Mniejszosc Niemiecka` = c(0, 
0, 0, 0, 0, 0), `19 - Mniejszosc Niemiecka Slaska` = c(0, 0, 
0, 0, 0, 0), Rok = c(2005, 2005, 2005, 2005, 2005, 2005)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))



  > sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

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    
system code page: 1250

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] zoo_1.8-12      writexl_1.4.2   plm_2.6-3       lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.2     purrr_1.0.1    
 [9] tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.2   tidyverse_2.0.0 dbplyr_2.3.3    readr_2.1.4     readxl_1.4.2   

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0  lattice_0.21-8    colorspace_2.1-0  vctrs_0.6.1       generics_0.1.3    htmltools_0.5.5   utf8_1.2.3       
 [8] rlang_1.1.0       pillar_1.9.0      later_1.3.0       glue_1.6.2        withr_2.5.0       DBI_1.1.3         lifecycle_1.0.3  
[15] munsell_0.5.0     gtable_0.3.3      cellranger_1.1.0  bdsmatrix_1.3-6   miscTools_0.6-28  tzdb_0.3.0        fastmap_1.1.1    
[22] httpuv_1.6.9      lmtest_0.9-40     parallel_4.1.3    fansi_1.0.4       Rcpp_1.0.10       xtable_1.8-4      promises_1.2.0.1 
[29] scales_1.2.1      mime_0.12         maxLik_1.5-2      hms_1.1.3         digest_0.6.31     stringi_1.7.12    collapse_1.9.5   
[36] rbibutils_2.2.13  grid_4.1.3        Rdpack_2.5        cli_3.4.1         tools_4.1.3       sandwich_3.0-2    magrittr_2.0.3   
[43] Formula_1.2-5     pkgconfig_2.0.3   ellipsis_0.3.2    MASS_7.3-58.3     timechange_0.2.0  rstudioapi_0.15.0 R6_2.5.1         
[50] nlme_3.1-162      compiler_4.1.3 

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

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    
system code page: 1250

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] zoo_1.8-12      writexl_1.4.2   plm_2.6-3       lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.2     purrr_1.0.1    
 [9] tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.2   tidyverse_2.0.0 dbplyr_2.3.3    readr_2.1.4     readxl_1.4.2   

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0  lattice_0.21-8    colorspace_2.1-0  vctrs_0.6.1       generics_0.1.3    htmltools_0.5.5   utf8_1.2.3       
 [8] rlang_1.1.0       pillar_1.9.0      later_1.3.0       glue_1.6.2        withr_2.5.0       DBI_1.1.3         lifecycle_1.0.3  
[15] munsell_0.5.0     gtable_0.3.3      cellranger_1.1.0  bdsmatrix_1.3-6   miscTools_0.6-28  tzdb_0.3.0        fastmap_1.1.1    
[22] httpuv_1.6.9      lmtest_0.9-40     parallel_4.1.3    fansi_1.0.4       Rcpp_1.0.10       xtable_1.8-4      promises_1.2.0.1 
[29] scales_1.2.1      mime_0.12         maxLik_1.5-2      hms_1.1.3         digest_0.6.31     stringi_1.7.12    collapse_1.9.5   
[36] rbibutils_2.2.13  grid_4.1.3        Rdpack_2.5        cli_3.4.1         tools_4.1.3       sandwich_3.0-2    magrittr_2.0.3   
[43] Formula_1.2-5     pkgconfig_2.0.3   ellipsis_0.3.2    MASS_7.3-58.3     timechange_0.2.0  rstudioapi_0.15.0 R6_2.5.1         
[50] nlme_3.1-162      compiler_4.1.3 
r 重命名

评论

2赞 jpsmith 10/26/2023
你能编辑你的问题以使其可重复吗?
0赞 Limey 10/26/2023
反引号 (') 与单引号 (') 不同。这是一个错别字。你需要反引号。
3赞 r2evans 10/26/2023
其中一项:(1)或来自不同的封装,屏蔽功能;(2)您的数据不继承;或 (3) 此处的代码不能完全代表您的真实代码。请编辑您的问题并添加 的输出,并将输出 添加到代码块中。谢谢!selectrenamedplyr::"data.frame"dput(head(wybory_2005_raw))sessionInfo()
1赞 Limey 10/26/2023
@r2evans我纠正了。我看到了单引号和不规则的列名,没有进一步看。
1赞 r2evans 10/26/2023
当我对你的数据运行你的代码时,我得到一个不同的错误:,因为你正在尝试重命名,但你的数据有名字(没有重音符号)。这就是为什么有一个可重现的问题很重要:我们无法重现你的错误,这意味着你向我们展示的代码对你的真实代码来说不够真实,所以你在这个过程中引入了其他不确定性。Can't subset columns that don't exist'6 - Prawo i Sprawiedliwość'`6 - Prawo i Sprawiedliwosc`

答: 暂无答案