无法在 R 中对因子变量执行重新编码

Unable to perform recoding in R for factor variables

我正在尝试将一些变量重新编码为因子以执行一些基本的可视化。但是,我遇到以下错误:

错误:

Error: `.vars` must be a character/numeric vector or a `vars()` object, not a `data.frame` object.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
`.vars` must be a character/numeric vector or a `vars()` object, not a `data.frame` object.
Backtrace:
 1. `%>%`(...)
 2. dplyr::mutate_at(...)
 3. dplyr:::manip_at(...)
 4. dplyr:::tbl_at_syms(.tbl, .vars, .include_group_vars = .include_group_vars)
 5. dplyr:::tbl_at_vars(tbl, vars, .include_group_vars = .include_group_vars)
 6. dplyr:::bad_args(...)
 7. dplyr:::glubort(fmt_args(args), ..., .envir = .envir)

我的代码:

d <- d %>% mutate_at(
  .vars = vars(contains("Dem_edu")),
  .funs = recode, 
  "- PhD/Doctorate" = "PHD", 
  "- Up to 12 years of school" = "12 years",
  "- Up to 9 years of school" = "9 years", 
  "- Up to 6 years of school" = "6 years",
  "- College degree" = "Col_degree",
  "- None" = "None",
  "- Some College or equivalent" = "Col",
  "1" = "1")

不确定我做错了什么。

dput(head(d))

的结果
> dput(head(d))
structure(list(StartDate = structure(c(1585219216, 1585219822, 
1585220572, 1585221981, 1585222622, 1585222239), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), EndDate = structure(c(1585220433, 1585220592, 1585221557, 
1585222697, 1585223366, 1585223396), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Status = c("IP Address", "IP Address", "IP Address", 
"IP Address", "IP Address", "IP Address"), Progress = c(100, 
100, 100, 100, 100, 100), `Duration (in seconds)` = c(1217, 770, 
984, 716, 743, 1156), Finished = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE), RecordedDate = structure(c(1585220434, 1585220593, 1585221558, 
1585222698, 1585223367, 1585223397), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), ResponseId = c("R_2uHCOahB4DrY5Mb", "R_sBeXDjmvUERjTqh", 
"R_3Dvlhb30AxXS5VX", "R_O6Fpln9BAtyuftv", "R_ZDXNVSta8CbZs89", 
"R_Uhfke7fjjClqv85"), DistributionChannel = c("anonymous", "anonymous", 
"anonymous", "anonymous", "anonymous", "anonymous"), UserLanguage = c("DA", 
"DA", "DA", "SCU", "DA", "FI"), Consent = c("Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes"), Dem_age = c(57, 54, 52, 27, 25, 38), Dem_gender = c("Female", 
"Male", "Female", "Female", "Male", "Female"), Dem_edu = c("- Up to 12 years of school", 
"- Some College, short continuing education or equivalent", "- Some College, short continuing education or equivalent", 
"- College degree, bachelor, master", "- College degree, bachelor, master", 
"- PhD/Doctorate"), Dem_edu_mom = c("- Up to 12 years of school", 
"- Some College or equivalent", "- Some College or equivalent", 
"- PhD/Doctorate", "- Up to 12 years of school", "- College degree"
), Dem_employment = c("Retired", "Full time employed", "Full time employed", 
"Student", "Student", "Full time employed"), Country = c("Denmark", 
"Denmark", "Denmark", "Canada", "Denmark", "Finland"), Dem_Expat = c("yes", 
"yes", "yes", "no", "yes", "yes"), Dem_state = c(NA, "København", 
"Skanderborg", "Ontario", "Københavns Kommune", NA), Dem_maritalstatus = c("Married/cohabiting", 
"Divorced/widowed", "Married/cohabiting", "Divorced/widowed", 
"Married/cohabiting", "Divorced/widowed"), Dem_dependents = c(0, 
0, 0, 0, 0, 0), Dem_riskgroup = c("Yes", "Not sure", NA, "Yes", 
"No", "Yes"), Dem_islolation = c("Life carries on with minor changes", 
"Life carries on with minor changes", "Isolated", "Isolated", 
"Life carries on with minor changes", "Life carries on with minor changes"
), Dem_isolation_adults = c(1, NA, 1, 1, 3, 1), Dem_isolation_kids = c(1, 
NA, 0, 0, 0, 0), AD_gain = c(NA, NA, NA, "· If Program A is adopted, 200 people will be saved.", 
"· If Program A is adopted, 200 people will be saved.", NA), 
    AD_loss = c("· If Program C is adopted 400 people will die.", 
    "· If Program C is adopted 400 people will die.", "· If Program D is adopted there is 1/3 probability that nobody will die, and 2/3 probability that 600 people will die.", 
    NA, NA, NA), AD_check = c("Not sure", "No", "Yes", "Yes", 
    "No", NA), Scale_PSS10_UCLA_1 = c("Very often", "Almost never", 
    "Fairly often", "Sometimes", "Never", NA), Scale_PSS10_UCLA_2 = c("Sometimes", 
    "Never", "Sometimes", "Sometimes", "Very often", NA), Scale_PSS10_UCLA_3 = c("Fairly often", 
    "Sometimes", "Fairly often", "Sometimes", "Almost never", 
    "Sometimes"), Scale_PSS10_UCLA_4 = c("Sometimes", "Very often", 
    "Very often", "Sometimes", "Sometimes", NA), Scale_PSS10_UCLA_5 = c("Sometimes", 
    "Fairly often", "Sometimes", "Sometimes", "Sometimes", NA
    ), Scale_PSS10_UCLA_6 = c("Sometimes", "Almost never", "Sometimes", 
    "Sometimes", "Fairly often", NA), Scale_PSS10_UCLA_7 = c("Sometimes", 
    "Fairly often", "Fairly often", "Sometimes", "Sometimes", 
    "Almost never"), Scale_PSS10_UCLA_8 = c("Sometimes", "Fairly often", 
    "Almost never", "Sometimes", "Sometimes", NA), Scale_PSS10_UCLA_9 = c("Very often", 
    "Sometimes", "Sometimes", "Sometimes", "Almost never", "Almost never"
    ), Scale_PSS10_UCLA_10 = c("Fairly often", "Never", "Sometimes", 
    "Sometimes", "Almost never", NA), Scale_PSS10_UCLA_11 = c("Fairly often", 
    "Almost never", "Sometimes", "Sometimes", "Fairly often", 
    NA), Scale_PSS10_UCLA_12 = c("Fairly often", "Almost never", 
    "Almost never", "Sometimes", "Never", NA), Scale_PSS10_UCLA_13 = c("Very often", 
    "Never", "Fairly often", "Sometimes", "Fairly often", NA), 
    OECD_people_1 = c(5, 7, 7, 0, 5, 3), OECD_people_2 = c(5, 
    9, 9, NA, 7, NA), OECD_insititutions_1 = c(5, 7, 8, NA, 8, 
    NA), OECD_insititutions_2 = c(5, 8, 9, NA, 7, NA), OECD_insititutions_3 = c(5, 
    8, 9, NA, 6, NA), OECD_insititutions_4 = c(1, 6, 8, NA, 7, 
    NA), OECD_insititutions_5 = c(NA, 6, 9, NA, 6, NA), OECD_insititutions_6 = c(5, 
    7, 8, NA, 6, NA), Corona_concerns_1 = c("Agree", "Slightly disagree", 
    "Slightly agree", "Strongly disagree", "Strongly disagree", 
    "Slightly disagree"), Corona_concerns_2 = c("Agree", "Disagree", 
    "Agree", "Strongly disagree", "Slightly disagree", NA), Corona_concerns_3 = c("Agree", 
    "Disagree", "Slightly agree", "Strongly disagree", "Strongly disagree", 
    "Slightly disagree"), Corona_concerns_4 = c("Strongly agree", 
    "Slightly agree", "Strongly agree", "Strongly disagree", 
    "Slightly agree", NA), Corona_concerns_5 = c("Strongly agree", 
    "Agree", "Strongly agree", "Strongly disagree", "Strongly disagree", 
    NA), Trust_countrymeasure = c("Too much", "8", "4", "Appropriate", 
    "7", "6"), Compliance_1 = c("Agree", "Strongly agree", "Strongly agree", 
    "Strongly disagree", "Strongly agree", NA), Compliance_2 = c("Agree", 
    "Agree", "Agree", "Strongly disagree", "Slightly agree", 
    NA), Compliance_3 = c("Agree", "Agree", "Agree", "Strongly disagree", 
    "Slightly agree", "Strongly disagree"), Compliance_4 = c("Agree", 
    "Strongly disagree", "Slightly disagree", "Strongly disagree", 
    "Slightly agree", "Disagree"), Compliance_5 = c("Agree", 
    "Agree", "Slightly agree", "Strongly disagree", "Slightly disagree", 
    NA), Compliance_6 = c("Slightly agree", "Slightly agree", 
    "Disagree", "Strongly disagree", "Slightly agree", NA), BFF_15_1 = c("Agree", 
    "Slightly agree", "Strongly agree", "Strongly disagree", 
    "Agree", NA), BFF_15_2 = c("Agree", "Slightly agree", "Agree", 
    "Strongly disagree", "Agree", NA), BFF_15_3 = c("Slightly agree", 
    "Slightly disagree", "Slightly agree", "Strongly disagree", 
    "Slightly disagree", NA), BFF_15_4 = c("Slightly disagree", 
    "Slightly disagree", "Slightly agree", "Strongly disagree", 
    "Slightly agree", NA), BFF_15_5 = c("Agree", "Slightly disagree", 
    "Slightly agree", "Strongly disagree", "Agree", NA), BFF_15_6 = c("Slightly agree", 
    "Agree", "Slightly agree", "Strongly disagree", "Slightly agree", 
    NA), BFF_15_7 = c("Strongly agree", "Slightly disagree", 
    "Agree", "Strongly disagree", "Slightly disagree", NA), BFF_15_8 = c("Strongly agree", 
    "Agree", "Agree", "Strongly disagree", "Strongly agree", 
    NA), BFF_15_9 = c("Slightly agree", "Agree", "Agree", "Strongly disagree", 
    "Slightly agree", NA), BFF_15_10 = c("Slightly agree", "Slightly disagree", 
    "Slightly agree", "Strongly disagree", "Disagree", NA), BFF_15_11 = c("Slightly agree", 
    "Disagree", "Slightly disagree", "Strongly disagree", "Slightly disagree", 
    NA), BFF_15_12 = c("Slightly agree", "Slightly disagree", 
    "Agree", "Strongly disagree", "Slightly disagree", NA), BFF_15_13 = c("Slightly disagree", 
    "Agree", "Strongly agree", "Strongly disagree", "Slightly agree", 
    NA), BFF_15_14 = c("Slightly disagree", "Slightly agree", 
    "Slightly disagree", "Strongly disagree", "Agree", NA), BFF_15_15 = c("Slightly agree", 
    "Slightly agree", "Slightly agree", "Strongly disagree", 
    "Slightly agree", NA), Expl_Distress_1 = c("Does not apply to my current situation", 
    "Disagree", "Slightly disagree", "Strongly disagree", "Strongly disagree", 
    NA), Expl_Distress_2 = c("Does not apply to my current situation", 
    "Strongly disagree", "Slightly agree", "Strongly disagree", 
    "Does not apply to my current situation", NA), Expl_Distress_3 = c("Does not apply to my current situation", 
    "Does not apply to my current situation", "Does not apply to my current situation", 
    "Strongly disagree", "Does not apply to my current situation", 
    NA), Expl_Distress_4 = c("Does not apply to my current situation", 
    "Slightly agree", "Agree", "Strongly disagree", "Agree", 
    NA), Expl_Distress_5 = c("Does not apply to my current situation", 
    "Does not apply to my current situation", "Does not apply to my current situation", 
    "Strongly disagree", "Does not apply to my current situation", 
    NA), Expl_Distress_6 = c("Strongly agree", "Strongly disagree", 
    "Agree", "Strongly disagree", "Strongly agree", NA), Expl_Distress_7 = c("Agree", 
    "Strongly disagree", "Does not apply to my current situation", 
    "Strongly disagree", "Strongly disagree", NA), Expl_Distress_8 = c("Slightly agree", 
    "Disagree", "Does not apply to my current situation", "Strongly disagree", 
    "Slightly agree", NA), Expl_Distress_9 = c("Does not apply to my current situation", 
    "Does not apply to my current situation", "Does not apply to my current situation", 
    "Strongly disagree", "Does not apply to my current situation", 
    NA), Expl_Distress_10 = c("Strongly agree", "Agree", "Strongly agree", 
    "Strongly disagree", "Slightly disagree", NA), Expl_Distress_11 = c("Strongly agree", 
    "Disagree", "Does not apply to my current situation", "Strongly disagree", 
    "Strongly disagree", NA), Expl_Distress_12 = c("Strongly agree", 
    "Disagree", "Slightly agree", "Strongly disagree", "Strongly disagree", 
    NA), Expl_Distress_13 = c("Strongly agree", "Slightly disagree", 
    "Slightly disagree", "Strongly disagree", "Strongly disagree", 
    NA), Expl_Distress_14 = c("Strongly agree", "Disagree", "Slightly disagree", 
    "Strongly disagree", "Disagree", NA), Expl_Distress_15 = c("Does not apply to my current situation", 
    "Disagree", "Slightly disagree", "Strongly disagree", "Does not apply to my current situation", 
    NA), Expl_Distress_16 = c("Strongly agree", "Slightly disagree", 
    "Agree", "Strongly disagree", "Slightly agree", NA), Expl_Distress_17 = c("Agree", 
    "Disagree", "Does not apply to my current situation", "Strongly disagree", 
    "Strongly disagree", NA), Expl_Distress_18 = c("Strongly agree", 
    "Strongly disagree", "Slightly agree", "Strongly disagree", 
    "Agree", NA), Expl_Distress_19 = c("Strongly agree", "Slightly agree", 
    "Slightly agree", "Strongly disagree", "Agree", NA), Expl_Distress_20 = c("Strongly agree", 
    "Slightly agree", "Disagree", "Strongly disagree", "Strongly agree", 
    NA), Expl_Distress_21 = c("Strongly agree", "Disagree", "Does not apply to my current situation", 
    "Strongly disagree", "Disagree", NA), Expl_Distress_22 = c("Does not apply to my current situation", 
    "Strongly disagree", "Does not apply to my current situation", 
    "Strongly disagree", "Strongly disagree", NA), Expl_Distress_23 = c("Strongly agree", 
    "Disagree", "Slightly agree", "Strongly disagree", "Strongly agree", 
    NA), Expl_Distress_24 = c("Strongly agree", "Strongly disagree", 
    "Agree", "Strongly disagree", "Strongly disagree", NA), Expl_Distress_txt = c(NA, 
    NA, NA, "Please exclude - test", NA, NA), SPS_1 = c("Agree", 
    "Slightly agree", "Agree", "Strongly disagree", "Slightly agree", 
    NA), SPS_2 = c("Slightly disagree", "Agree", "Agree", "Strongly disagree", 
    "Disagree", NA), SPS_3 = c("Slightly disagree", "Agree", 
    "Slightly disagree", "Strongly disagree", "Slightly agree", 
    NA), SPS_4 = c("Slightly disagree", "Agree", "Slightly agree", 
    "Strongly disagree", "Slightly agree", NA), SPS_5 = c("Agree", 
    "Agree", "Slightly agree", "Strongly disagree", "Slightly agree", 
    NA), SPS_6 = c("Disagree", "Strongly agree", "Slightly agree", 
    "Strongly disagree", "Slightly disagree", NA), SPS_7 = c("Agree", 
    "Agree", "Slightly agree", "Strongly disagree", "Agree", 
    NA), SPS_8 = c("Disagree", "Slightly agree", "Agree", "Strongly disagree", 
    "Slightly disagree", NA), SPS_9 = c("Slightly agree", "Agree", 
    "Slightly agree", "Strongly disagree", "Slightly disagree", 
    NA), SPS_10 = c(NA, "Agree", "Agree", "Strongly disagree", 
    "Agree", NA), Expl_Coping_1 = c("Agree", "Agree", "Agree", 
    "Strongly disagree", "Slightly disagree", NA), Expl_Coping_2 = c("Strongly disagree", 
    "Slightly disagree", "Slightly agree", "Strongly disagree", 
    "Agree", NA), Expl_Coping_3 = c("Agree", "Slightly agree", 
    "Strongly agree", "Strongly disagree", "Slightly agree", 
    NA), Expl_Coping_4 = c("Strongly agree", "Slightly disagree", 
    "Disagree", "Strongly disagree", "Strongly disagree", NA), 
    Expl_Coping_5 = c("Strongly disagree", "Agree", "Strongly agree", 
    "Strongly disagree", "Slightly disagree", NA), Expl_Coping_6 = c("Strongly disagree", 
    "Agree", "Strongly agree", "Strongly disagree", "Slightly disagree", 
    NA), Expl_Coping_7 = c("Agree", "Agree", "Slightly disagree", 
    "Strongly disagree", "Slightly agree", NA), Expl_Coping_8 = c("Agree", 
    "Disagree", "Slightly disagree", "Strongly disagree", "Agree", 
    NA), Expl_Coping_9 = c("Agree", "Agree", "Agree", "Strongly disagree", 
    "Slightly agree", NA), Expl_Coping_10 = c("Slightly disagree", 
    "Slightly disagree", "Slightly disagree", "Strongly disagree", 
    "Strongly disagree", NA), Expl_Coping_11 = c("Agree", "Agree", 
    "Slightly agree", "Strongly disagree", "Slightly agree", 
    NA), Expl_Coping_12 = c("Agree", "Agree", "Strongly agree", 
    "Strongly disagree", "Slightly agree", NA), Expl_Coping_13 = c("Strongly disagree", 
    "Agree", "Agree", "Strongly disagree", "Slightly disagree", 
    NA), Expl_Coping_14 = c("Agree", "Agree", "Agree", "Strongly disagree", 
    "Slightly agree", NA), Expl_Coping_15 = c("Slightly disagree", 
    "Strongly disagree", "Disagree", "Strongly disagree", "Strongly disagree", 
    NA), Expl_Coping_16 = c("Agree", "Agree", "Strongly agree", 
    "Strongly disagree", "Slightly disagree", NA), Expl_coping_txt = c(NA, 
    NA, NA, "Please exclude - testing", NA, NA), Expl_media_1 = c("Strongly agree", 
    "Disagree", "Slightly agree", "Strongly disagree", "Agree", 
    NA), Expl_media_2 = c("Strongly agree", "Disagree", "Slightly disagree", 
    "Strongly disagree", "Strongly disagree", NA), Expl_media_3 = c("Strongly agree", 
    "Disagree", "Disagree", "Strongly disagree", "Agree", NA), 
    Expl_media_4 = c("Strongly disagree", "Disagree", "Strongly agree", 
    "Strongly disagree", "Disagree", NA), Expl_media_5 = c("Strongly disagree", 
    "Disagree", "Strongly agree", "Strongly disagree", "Slightly disagree", 
    NA), Expl_media_6 = c("Strongly disagree", "Agree", "Agree", 
    "Strongly disagree", "Slightly agree", NA), Final_open = c(NA, 
    NA, "Har altid levet med mild angst og lavt selvværd. Denne situation forværrer angsten.", 
    "Please exclude - testing", NA, NA)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))

Session 信息:

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252   
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
[5] LC_TIME=English_India.1252    

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

other attached packages:
 [1] expss_0.10.7    multicon_1.6    foreach_1.5.1   abind_1.4-5     psych_2.1.3    
 [6] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.5     purrr_0.3.4     readr_1.4.0    
[11] tidyr_1.1.3     tibble_3.1.0    ggplot2_3.3.3   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         lubridate_1.7.10   mvtnorm_1.1-1      lattice_0.20-41   
 [5] digest_0.6.27      assertthat_0.2.1   utf8_1.2.1         R6_2.5.0          
 [9] cellranger_1.1.0   backports_1.2.1    reprex_2.0.0       httr_1.4.2        
[13] pillar_1.6.0       rlang_0.4.10       readxl_1.3.1       rstudioapi_0.13   
[17] data.table_1.14.0  checkmate_2.0.0    labeling_0.4.2     foreign_0.8-81    
[21] htmlwidgets_1.5.3  munsell_0.5.0      broom_0.7.5        xfun_0.22         
[25] compiler_4.0.5     modelr_0.1.8       pkgconfig_2.0.3    mnormt_2.0.2      
[29] tmvnsim_1.0-2      htmltools_0.5.1.1  sciplot_1.2-0      tidyselect_1.1.0  
[33] htmlTable_2.1.0    matrixStats_0.58.0 codetools_0.2-18   fansi_0.4.2       
[37] crayon_1.4.1       dbplyr_2.1.0       withr_2.4.1        grid_4.0.5        
[41] nlme_3.1-152       jsonlite_1.7.2     gtable_0.3.0       lifecycle_1.0.0   
[45] DBI_1.1.1          magrittr_2.0.1     scales_1.1.1       cli_2.4.0         
[49] stringi_1.5.3      farver_2.1.0       fs_1.5.0           xml2_1.3.2        
[53] ellipsis_0.3.1     generics_0.1.0     vctrs_0.3.6        iterators_1.0.13  
[57] tools_4.0.5        glue_1.4.2         hms_1.0.0          parallel_4.0.5    
[61] yaml_2.2.1         colorspace_2.0-0   rvest_1.0.0        knitr_1.31        
[65] haven_2.3.1

首先尝试dplyr::mutate...

无论如何: 使用 across 而不是 mutate_at -> 作用域动词 (_if, _at, _all) 已被现有动词中的 across() 取代

此代码已明确显示在您的 Dem_edu 专栏中,现在应该可以使用了

df %>% 
  select(contains("Dem_edu")) %>% 
  mutate(across(starts_with("Dem_edu"), as.factor),
         across(starts_with("Dem_edu"), ~recode(., "- PhD/Doctorate" = "PHD", 
                                   "- Up to 12 years of school" = "12 years",
                                   "- Up to 9 years of school" = "9 years", 
                                   "- Up to 6 years of school" = "6 years",
                                   "- College degree" = "Col_degree",
                                   "- None" = "None",
                                   "- Some College or equivalent" = "Col",
                                   "1" = "1"))
  )

输出:

  Dem_edu                                                  Dem_edu_mom
  <fct>                                                    <fct>      
1 12 years                                                 12 years   
2 - Some College, short continuing education or equivalent Col        
3 - Some College, short continuing education or equivalent Col        
4 - College degree, bachelor, master                       PHD        
5 - College degree, bachelor, master                       12 years   
6 PHD                                                      Col_degree