Преобразование набора данных из широкого в длинный с использованием pivot_longer, но возвращается ошибка, говорящая, что x не является вектором

Я пытаюсь преобразовать довольно длинный набор данных, который я построил, в длинный формат, используя «_» в качестве разделителя и суффиксы в качестве лет (1b — 2018 год, а 2 — 2020 год). Я построил код следующим образом:

GSS_ANES_long <- GSS_ANES %>%
  select(!c(year_1b,year_2)) %>%
  pivot_longer(
    cols = -c(samptype, yearid, fileversion, panstat, anesid, version, V200001, V200017b, V200017c, V200017d, V202022, V202352, V202470, V202542, V202543, V202544, V202545, V202546, V202547, V202629, V202630),
    names_sep = "_",
    names_to = c(".value", "year"),
    names_repair = "minimal")

Который работал на предыдущей версии моего набора данных. Однако, поработав еще немного с ним в STATA и снова запустив его в R (я знаю, что это не имеет смысла, но я привык к созданию переменных в STATA и запуску моделей в R), он вернул следующую ошибку.

Error in `vec_slice()`:
! `x` must be a vector, not `NULL`.
Run `rlang::last_error()` to see where the error occurred.

Я знаю, что это значит, но я не очень уверен, как я могу решить эту проблему, и ответы, уже опубликованные здесь (по крайней мере, те, которые я нашел), были слишком специфичны для данных или слишком широки, чтобы просто знать, как вращаться.

Ниже я приведу пример кода, взятого из первых 5 строк моего набора данных. Сами данные содержат много пропущенных значений, так что потерпите меня.

structure(list(samptype = structure(c(2016, 2016, 2016, 2016, 
2016), format.stata = "%8.0g", labels = c(`sample from gss 2016` = 2016, 
`sample from gss 2018` = 2018), class = c("haven_labelled", "vctrs_vctr", 
"double")), yearid = structure(c(20160001, 20160002, 20160003, 
20160004, 20160005), format.stata = "%12.0g"), fileversion = structure(c("GSS 2020 Panel Release 1 (May 2021)", 
"GSS 2020 Panel Release 1 (May 2021)", "GSS 2020 Panel Release 1 (May 2021)", 
"GSS 2020 Panel Release 1 (May 2021)", "GSS 2020 Panel Release 1 (May 2021)"
), format.stata = "%35s"), panstat = structure(c(1, 1, 0, 1, 
0), format.stata = "%8.0g", labels = c(`not selected` = 0, `selected, eligible, and reinterviewed` = 1, 
`selected, but not reinterviewed` = 2, `selected, but not eligible and not reinterviewed` = 3, 
`selected, but not eligible and not reinterviewed because r lived outside us` = 31, 
`selected, but not eligible and not reinterviewed because r was in institution` = 32, 
`selected, but not eligible and not reinterviewed because r was deceased` = 33, 
`selected, but not eligible and not reinterviewed because r was permanently incapacitated` = 34
), class = c("haven_labelled", "vctrs_vctr", "double")), wtssall_1b = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), label = "weight variable", format.stata = "%12.0g"), 
    wtssall_2 = structure(c(1.08500894295449, 0.542504471477243, 
    NA, 2.17001788590897, NA), label = "weight variable", format.stata = "%12.0g"), 
    wtssnr_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "weight variable", format.stata = "%12.0g"), 
    wtssnr_2 = structure(c(1.44392875550612, 0.721964377753061, 
    NA, 2.88785751101224, NA), label = "weight variable", format.stata = "%12.0g"), 
    vstrat_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "variance stratum", format.stata = "%8.0g"), 
    vstrat_2 = structure(c(3201, 3201, NA, 3201, NA), label = "variance stratum", format.stata = "%8.0g"), 
    vpsu_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "variance primary sampling unit", format.stata = "%8.0g"), 
    vpsu_2 = structure(c(1, 1, NA, 1, NA), label = "variance primary sampling unit", format.stata = "%8.0g"), 
    year_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "gss year for this respondent", format.stata = "%8.0g"), 
    year_2 = structure(c(2020, 2020, NA, 2020, NA), label = "gss year for this respondent", format.stata = "%8.0g"), 
    id_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "respondent id number", format.stata = "%8.0g"), 
    id_2 = structure(c(1, 2, NA, 3, NA), label = "respondent id number", format.stata = "%8.0g"), 
    age_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "age of respondent", format.stata = "%8.0g", labels = c(`89 or older` = 89), class = c("haven_labelled", 
    "vctrs_vctr", "double")), attend_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how often r attends religious services", format.stata = "%8.0g", labels = c(never = 0, 
    `less than once a year` = 1, `about once or twice a year` = 2, 
    `several times a year` = 3, `about once a month` = 4, `2-3 times a year` = 5, 
    `nearly every week` = 6, `every week` = 7, `several times a week` = 8
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    fair_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "people fair or try to take advantage", format.stata = "%21.0g", labels = c(`People take advantage` = 0, 
    `People are fair` = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), happy_1b = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), label = "general happiness", format.stata = "%8.0g", labels = c(`very happy` = 1, 
    `pretty happy` = 2, `not too happy` = 3), class = c("haven_labelled", 
    "vctrs_vctr", "double")), health_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "condition of health", format.stata = "%8.0g", labels = c(excellent = 1, 
    good = 2, fair = 3, poor = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), helpful_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "people helpful or looking out for selves", format.stata = "%11.0g", labels = c(`Not Helpful` = 0, 
    Helpful = 1), class = c("haven_labelled", "vctrs_vctr", "double"
    )), marcohab_1b = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), label = "cohabitation status", format.stata = "%8.0g", labels = c(married = 1, 
    `not married, cohabitating partner` = 2, `not married, no cohabitating partner` = 3, 
    `not married, missing on cohabitating` = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), marital_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "marital status", format.stata = "%8.0g", labels = c(married = 1, 
    widowed = 2, divorced = 3, separated = 4, `never married` = 5
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    realrinc_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "r's income in constant $", format.stata = "%12.0g"), 
    region_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "region of interview", format.stata = "%8.0g", labels = c(`new england` = 1, 
    `middle atlantic` = 2, `east north central` = 3, `west north central` = 4, 
    `south atlantic` = 5, `east south atlantic` = 6, `west south central` = 7, 
    mountain = 8, pacific = 9), class = c("haven_labelled", "vctrs_vctr", 
    "double")), rincome_1b = structure(c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), label = "respondents income", format.stata = "%8.0g", labels = c(`under $1,000` = 1, 
    `$1,000 to $2,999` = 2, `$3,000 to $3,999` = 3, `$4,000 to $4,999` = 4, 
    `$5,000 to $5,999` = 5, `$6,000 to $6,999` = 6, `$7,000 to $7,999` = 7, 
    `$8,000 to $9,999` = 8, `$10,000 to $14,999` = 9, `$15,000 to $19,999` = 10, 
    `$20,000 to $24,999` = 11, `$25,000 or more` = 12, refused = 13
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    socbar_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "spend evening at bar", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socfrend_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with friends", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socommun_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with neighbor", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socrel_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with relatives", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), trust_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "can people be trusted", format.stata = "%21.0g", labels = c(`Can't be too careful` = 0, 
    `People can be trusted` = 1), class = c("haven_labelled", 
    "vctrs_vctr", "double")), uscitzn_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "is r us citizen", format.stata = "%8.0g", labels = c(`a u.s. citizen` = 1, 
    `not a u.s. citizen` = 2, `a u.s. citizen born in puerto rico, the u.s. virgin islands, or the northern marianas islands (if volunteered)` = 3, 
    `born outside of the u.s. to parents who were u.s. citizens at that time (if volunteered)` = 4
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    wwwhr_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), label = "www hours per week", format.stata = "%8.0g", labels = c(`0 hours` = 0, 
    `168 hours` = 168), class = c("haven_labelled", "vctrs_vctr", 
    "double")), conf2f_1b = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), label = "how many people r sees face to face", format.stata = "%8.0g", labels = c(`all or almost all of them` = 1, 
    `most of them` = 2, `about half of them` = 3, `some of them` = 4, 
    `none or almost none of them` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), conwkday_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "number of people r contacts with on a typical weekday", format.stata = "%18.0g", labels = c(`0-4 people` = 1, 
    `5-9 people` = 2, `10-19 people` = 3, `20-49 people` = 4, 
    `50 or more people` = 5, `100 or more people` = 6), class = c("haven_labelled", 
    "vctrs_vctr", "double")), intcntct_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how much of r's communication is via text, mobile phone, or internet", format.stata = "%32.0g", labels = c(`Low or Mid-Level Online Presence` = 0, 
    `High Online Presence` = 1), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely1_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt they lack companionship", format.stata = "%8.0g", labels = c(iap = NA_real_, 
    `not available for this version of the data file` = NA_real_, 
    `not available for this year` = NA_real_), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely2_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt isolated from others", format.stata = "%8.0g", labels = c(iap = NA_real_, 
    `not available for this version of the data file` = NA_real_, 
    `not available for this year` = NA_real_), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely3_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt left out", format.stata = "%8.0g", labels = c(never = 1, 
    rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), partpartonline_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "in past 12 months, r has participated in orgs for politics or political associat", format.stata = "%16.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), partvol_1b = structure(c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), label = "in past 12 months, r has participated in charitable or religious volunteer orgs", format.stata = "%16.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), age_2 = structure(c(51, 65, NA, 47, NA), label = "age of respondent", format.stata = "%8.0g", labels = c(`89 or older` = 89), class = c("haven_labelled", 
    "vctrs_vctr", "double")), attend_2 = structure(c(1, 1, NA, 
    4, NA), label = "how often r attends religious services", format.stata = "%8.0g", labels = c(never = 0, 
    `less than once a year` = 1, `about once or twice a year` = 2, 
    `several times a year` = 3, `about once a month` = 4, `2-3 times a year` = 5, 
    `nearly every week` = 6, `every week` = 7, `several times a week` = 8
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    fair_2 = structure(c(NA, 1, NA, NA, NA), label = "people fair or try to take advantage", format.stata = "%21.0g", labels = c(`People take advantage` = 0, 
    `People are fair` = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), happy_2 = structure(c(2, 2, NA, 2, NA), label = "general happiness", format.stata = "%8.0g", labels = c(`very happy` = 1, 
    `pretty happy` = 2, `not too happy` = 3), class = c("haven_labelled", 
    "vctrs_vctr", "double")), health_2 = structure(c(3, NA, NA, 
    2, NA), label = "condition of health", format.stata = "%8.0g", labels = c(excellent = 1, 
    good = 2, fair = 3, poor = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), helpful_2 = structure(c(NA, 0, 
    NA, NA, NA), label = "people helpful or looking out for selves", format.stata = "%11.0g", labels = c(`Not Helpful` = 0, 
    Helpful = 1), class = c("haven_labelled", "vctrs_vctr", "double"
    )), marcohab_2 = structure(c(1, 3, NA, 1, NA), label = "cohabitation status", format.stata = "%8.0g", labels = c(married = 1, 
    `not married, cohabitating partner` = 2, `not married, no cohabitating partner` = 3, 
    `not married, missing on cohabitating` = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), marital_2 = structure(c(1, 5, NA, 
    1, NA), label = "marital status", format.stata = "%8.0g", labels = c(married = 1, 
    widowed = 2, divorced = 3, separated = 4, `never married` = 5
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    realrinc_2 = structure(c(147659.41804, 23980, NA, NA, NA), label = "r's income in constant $", format.stata = "%12.0g"), 
    region_2 = structure(c(1, 1, NA, 1, NA), label = "region of interview", format.stata = "%8.0g", labels = c(`new england` = 1, 
    `middle atlantic` = 2, `east north central` = 3, `west north central` = 4, 
    `south atlantic` = 5, `east south atlantic` = 6, `west south central` = 7, 
    mountain = 8, pacific = 9), class = c("haven_labelled", "vctrs_vctr", 
    "double")), rincome_2 = structure(c(13, 12, NA, NA, NA), label = "respondents income", format.stata = "%8.0g", labels = c(`under $1,000` = 1, 
    `$1,000 to $2,999` = 2, `$3,000 to $3,999` = 3, `$4,000 to $4,999` = 4, 
    `$5,000 to $5,999` = 5, `$6,000 to $6,999` = 6, `$7,000 to $7,999` = 7, 
    `$8,000 to $9,999` = 8, `$10,000 to $14,999` = 9, `$15,000 to $19,999` = 10, 
    `$20,000 to $24,999` = 11, `$25,000 or more` = 12, refused = 13
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    socbar_2 = structure(c(3, 4, NA, 2, NA), label = "spend evening at bar", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socfrend_2 = structure(c(3, 3, 
    NA, 2, NA), label = "spend evening with friends", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socommun_2 = structure(c(1, 1, 
    NA, 3, NA), label = "spend evening with neighbor", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), socrel_2 = structure(c(3, 3, NA, 
    3, NA), label = "spend evening with relatives", format.stata = "%9.0g", labels = c(Often = 1, 
    Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), trust_2 = structure(c(NA, 1, NA, 
    NA, NA), label = "can people be trusted", format.stata = "%21.0g", labels = c(`Can't be too careful` = 0, 
    `People can be trusted` = 1), class = c("haven_labelled", 
    "vctrs_vctr", "double")), uscitzn_2 = structure(c(1, 1, NA, 
    1, NA), label = "is r us citizen", format.stata = "%8.0g", labels = c(`a u.s. citizen` = 1, 
    `not a u.s. citizen` = 2, `a u.s. citizen born in puerto rico, the u.s. virgin islands, or the northern marianas islands (if volunteered)` = 3, 
    `born outside of the u.s. to parents who were u.s. citizens at that time (if volunteered)` = 4
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    wwwhr_2 = structure(c(20, 10, NA, 2, NA), label = "www hours per week", format.stata = "%8.0g", labels = c(`0 hours` = 0, 
    `168 hours` = 168), class = c("haven_labelled", "vctrs_vctr", 
    "double")), conf2f_2 = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), label = "how many people r sees face to face", format.stata = "%8.0g", labels = c(iap = NA_real_, 
    `not available for this version of the data file` = NA_real_, 
    `not available for this year` = NA_real_), class = c("haven_labelled", 
    "vctrs_vctr", "double")), conwkday_2 = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "number of people r contacts with on a typical weekday", format.stata = "%8.0g", labels = c(iap = NA_real_, 
    `not available for this version of the data file` = NA_real_, 
    `not available for this year` = NA_real_), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely1_2 = structure(c(2, 3, NA, 
    1, NA), label = "how often in the past 4 weeks r has felt they lack companionship", format.stata = "%8.0g", labels = c(never = 1, 
    rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely2_2 = structure(c(1, 1, NA, 
    3, NA), label = "how often in the past 4 weeks r has felt isolated from others", format.stata = "%8.0g", labels = c(never = 1, 
    rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely3_2 = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt left out", format.stata = "%8.0g", labels = c(iap = NA_real_, 
    `not available for this version of the data file` = NA_real_, 
    `not available for this year` = NA_real_), class = c("haven_labelled", 
    "vctrs_vctr", "double")), anesid = structure(c(169657, 169664, 
    NA, NA, NA), format.stata = "%10.0g"), version = structure(c("ANES-GSS_2020JointStudy_20220408", 
    "ANES-GSS_2020JointStudy_20220408", "", "", ""), label = "Version of ANES-GSS 2020 Joint Study Release", format.stata = "%32s"), 
    V200001 = structure(c(169657, 169664, NA, NA, NA), label = "2020 Case ID", format.stata = "%12.0g"), 
    V200017b = structure(c(1.40270924414924, 1.58917078954157, 
    NA, NA, NA), label = "GSS sample post-election weight", format.stata = "%12.0g"), 
    V200017c = structure(c(1, 1, NA, NA, NA), label = "GSS sample variance unit", format.stata = "%12.0g"), 
    V200017d = structure(c(1, 1, NA, NA, NA), label = "GSS sample variance stratum", format.stata = "%12.0g"), 
    V202022 = structure(c(1, 1, NA, NA, NA), label = "POST: R ever discuss politics with family or friends", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `1. Yes` = 1, `2. No` = 2), class = c("haven_labelled", "vctrs_vctr", 
    "double")), V202352 = structure(c(4, 2, NA, NA, NA), label = "POST: How would R describe social class [EGSS]", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-8. Don't know` = -8, `-5. Interview breakoff (sufficient partial IW)` = -5, 
    `1. Lower class` = 1, `2. Working class` = 2, `3. Middle class` = 3, 
    `4. Upper class` = 4), class = c("haven_labelled", "vctrs_vctr", 
    "double")), V202470 = structure(c(3, 3, NA, NA, NA), label = "POST: R currently smoking", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Every day` = 1, `2. Some days` = 2, `3. Not at all` = 3
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    V202542 = structure(c(3, NA, NA, NA, NA), label = "POST: How often use Facebook", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Many times every day` = 1, `2. A few times every day` = 2, 
    `3. About once a day` = 3, `4. A few times each week` = 4, 
    `5. About once a week` = 5, `6. Once or twice a month` = 6, 
    `7. Less than once a month` = 7), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202543 = structure(c(5, NA, NA, 
    NA, NA), label = "POST: How often post political content on Facebook", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3, 
    `4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202544 = structure(c(6, NA, NA, 
    NA, NA), label = "POST: How often use Twitter", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Many times every day` = 1, `2. A few times every day` = 2, 
    `3. About once a day` = 3, `4. A few times each week` = 4, 
    `5. About once a week` = 5, `6. Once or twice a month` = 6, 
    `7. Less than once a month` = 7), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202545 = structure(c(5, NA, NA, 
    NA, NA), label = "POST: How often post political content on Twitter", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3, 
    `4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202546 = structure(c(2, NA, NA, 
    NA, NA), label = "POST: How often use Reddit", format.stata = "%12.0g", labels = c(`-5. Interview breakoff (sufficient partial IW)` = -5, 
    `-1. Inapplicable` = -1, `1. Many times every day` = 1, `2. A few times every day` = 2, 
    `3. About once a day` = 3, `4. A few times each week` = 4, 
    `5. About once a week` = 5, `6. Once or twice a month` = 6, 
    `7. Less than once a month` = 7), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202547 = structure(c(5, NA, NA, 
    NA, NA), label = "POST: How often post political content on Reddit", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1, 
    `1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3, 
    `4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), V202629 = structure(c(1, 3, NA, 
    NA, NA), label = "POST: GSS: In past seven days has R been bothered by emotional problems", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `1. Never` = 1, 
    `2. Rarely` = 2, `3. Sometimes` = 3, `4. Often` = 4, `5. Always` = 5
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    V202630 = structure(c(3, 3, NA, NA, NA), label = "POST: GSS: Taken all together how happy is R these days", format.stata = "%12.0g", labels = c(`-9. Refused` = -9, 
    `-5. Interview breakoff (sufficient partial IW)` = -5, `1. Very happy` = 1, 
    `2. Pretty happy` = 2, `3. Not too happy` = 3), class = c("haven_labelled", 
    "vctrs_vctr", "double")), `_merge` = structure(c(3, 3, 1, 
    1, 1), label = "Matching result from merge", format.stata = "%23.0g", labels = c(`Master only (1)` = 1, 
    `Using only (2)` = 2, `Matched (3)` = 3, `Missing updated (4)` = 4, 
    `Nonmissing conflict (5)` = 5), class = c("haven_labelled", 
    "vctrs_vctr", "double")), agecat_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "Age in Categories", format.stata = "%9.0g", labels = c(`18-25` = 1, 
    `26-45` = 2, `46-64` = 3, `65+` = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), agecat_2 = structure(c(3, 4, NA, 
    3, NA), label = "Age in Categories", format.stata = "%9.0g", labels = c(`18-25` = 1, 
    `26-45` = 2, `46-64` = 3, `65+` = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), region4_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "region of interview (4 regions)", format.stata = "%10.0g", labels = c(`North-East` = 1, 
    Midwest = 2, South = 3, West = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), region4_2 = structure(c(1, 1, NA, 
    1, NA), label = "region of interview (4 regions)", format.stata = "%10.0g", labels = c(`North-East` = 1, 
    Midwest = 2, South = 3, West = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), attend4_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "frequency of religious attendance", format.stata = "%9.0g", labels = c(Never = 1, 
    Rarely = 2, Sometimes = 3, Often = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), attend4_2 = structure(c(2, 2, NA, 
    3, NA), label = "frequency of religious attendance", format.stata = "%9.0g", labels = c(Never = 1, 
    Rarely = 2, Sometimes = 3, Often = 4), class = c("haven_labelled", 
    "vctrs_vctr", "double")), lonely_1b = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "Loneliness Scale (Physical and Emotional)", format.stata = "%28.0g", labels = c(Rarely = 1, 
    Sometimes = 2, Often = 3), class = c("haven_labelled", "vctrs_vctr", 
    "double")), lonely_2 = structure(c(1, 1, NA, 1, NA), label = "Loneliness Scale (Physical and Emotional)", format.stata = "%21.0g", labels = c(Rarely = 1, 
    Sometimes = 2, Often = 3), class = c("haven_labelled", "vctrs_vctr", 
    "double")), cohesion_1b = structure(c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), label = "Social Cohesion Index based on Fair, Helpful, and Trust", format.stata = "%55.0g", labels = c(`Not Fair, Not Helpful, Not Trustworthy` = 1, 
    `At least two No` = 2, `At least two Yes` = 3, `Fair, Helpful, and Trustworthy` = 4
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    cohesion_2 = structure(c(NA, 3, NA, NA, NA), label = "Social Cohesion Index based on Fair, Helpful, and Trust", format.stata = "%55.0g", labels = c(`Not Fair, Not Helpful, Not Trustworthy` = 1, 
    `At least two No` = 2, `At least two Yes` = 3, `Fair, Helpful, and Trustworthy` = 4
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    partpartoffline_1b = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), label = "past 12 months, r has participated in orgs for politics or political assoc.", format.stata = "%16.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), partpartoffline_2 = structure(c(0, 0, NA, NA, 
    NA), label = "past 12 months, r has participated in political activities or orgs offline", format.stata = "%184.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), partpartonline_2 = structure(c(0, 0, NA, NA, 
    NA), label = "past 12 months, r has participated in political activities or orgs online", format.stata = "%182.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), partvol_2 = structure(c(0, 0, NA, NA, NA), label = "in past 12 months, r has participated in charitable or religious volunteer orgs", format.stata = "%88.0g", labels = c(`Not Participated` = 0, 
    Participated = 1), class = c("haven_labelled", "vctrs_vctr", 
    "double")), intcntct_2 = structure(c(1, 0, NA, NA, NA), label = "how much of r's communication is via text, mobile phone, or internet", format.stata = "%148.0g", labels = c(`Low or Mid-Level Online Presence` = 0, 
    `High Online Presence` = 1), class = c("haven_labelled", 
    "vctrs_vctr", "double"))), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

Я попытался снова просмотреть переменные как в STATA, так и в R, чтобы проверить, не допустил ли я где-то ошибку, но я еще не нашел решения этой проблемы. Я специально не прошу решения, но даже просто указать направление, с которого я могу начать искать ошибки, будет невероятно полезно.

Обновлено: я подумал, что было бы полезно поделиться тем, что у меня есть новая ошибка:

Error:
! Column 33 must be named.
Use .name_repair to specify repair.
Caused by error in `repaired_names()`:
! Names can't be empty.
✖ Empty name found at location 33.
Стоит ли изучать PHP в 2023-2024 годах?
Стоит ли изучать PHP в 2023-2024 годах?
Привет всем, сегодня я хочу высказать свои соображения по поводу вопроса, который я уже много раз получал в своем сообществе: "Стоит ли изучать PHP в...
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
В JavaScript одним из самых запутанных понятий является поведение ключевого слова "this" в стрелочной и обычной функциях.
Приемы CSS-макетирования - floats и Flexbox
Приемы CSS-макетирования - floats и Flexbox
Здравствуйте, друзья-студенты! Готовы совершенствовать свои навыки веб-дизайна? Сегодня в нашем путешествии мы рассмотрим приемы CSS-верстки - в...
Тестирование функциональных ngrx-эффектов в Angular 16 с помощью Jest
В системе управления состояниями ngrx, совместимой с Angular 16, появились функциональные эффекты. Это здорово и делает код определенно легче для...
Концепция локализации и ее применение в приложениях React ⚡️
Концепция локализации и ее применение в приложениях React ⚡️
Локализация - это процесс адаптации приложения к различным языкам и культурным требованиям. Это позволяет пользователям получить опыт, соответствующий...
Пользовательский скаляр GraphQL
Пользовательский скаляр GraphQL
Листовые узлы системы типов GraphQL называются скалярами. Достигнув скалярного типа, невозможно спуститься дальше по иерархии типов. Скалярный тип...
1
0
56
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

У вас есть столбец с именем _merge, который вызывает проблему. Переименуйте его или исключите в col и пивот заработает.

Редактировать:

В этом фрагменте кода все столбцы, начинающиеся с _, такие как _merge, удаляются из фрейма. Тогда только те, которые содержат подчеркивание, используются для поворота.

GSS_ANES %>%
  select(!c(year_1b,year_2, starts_with("_"))) %>% # Remove any cols starting with an underscore
  pivot_longer(
    cols = contains("_"), # Only use cols containing the separator for the pivot
    names_sep = "_",
    names_to = c(".value", "year"),
    names_repair = "minimal"
    )

Я действительно пытался это сделать, но все равно возвращал ту же ошибку.

mike 03.02.2023 22:08

Та же ошибка или другая? Если вы поместите _merge в вектор col, вы должны указать его в кавычках, иначе это не удастся

shs 03.02.2023 22:19

Я сделал это, используя select(), чтобы полностью удалить столбец. Ошибка была такая же.

mike 03.02.2023 23:00

Какие у вас версии R и тайдыр? Вы запускали свой код на полном кадре или на выводе dput, который вы разместили здесь?

shs 03.02.2023 23:41

Моя версия R — 4.2.2. У меня была установлена ​​более старая версия tidyr, поэтому я обновился до версии 1.3, и она действительно что-то изменила. Однако теперь появилась новая ошибка, говорящая о том, что мой столбец 33 не назван (а это так). Я запускаю код на полном кадре.

mike 04.02.2023 14:55

Ваш разделитель «_», и у вас есть names_to = c(".value", "year"). Имя столбца, такое как "_merge", вызовет именно эту ошибку, поскольку слева от разделителя есть только пустая строка, и сводная точка попытается создать имя столбца на основе этого. Проверьте еще раз, действительно ли вы удалили столбец _merge и нет ли больше подобных ему в кадре, на котором вы выполняете поворот.

shs 04.02.2023 15:05

Я отредактировал фрагмент в своем ответе, попробуйте, если это сработает на ваших данных. Если это не так, также попробуйте это на более коротком кадре dput, который вы разместили, чтобы увидеть, не заключается ли проблема в разнице между ними.

shs 04.02.2023 17:09

Это сработало, спасибо! Мои ошибки были комбинацией устаревшего тайдыра и переменной _merge, вызывающей проблемы.

mike 04.02.2023 22:13

Другие вопросы по теме