I am trying to convert a pretty long dataset I built into long format, using the "_" as a separator and the suffixes as the years (1b is 2018 and 2 is 2020). I've built the code as follows:
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")
Which worked on a previous version of my dataset. However, after tinkering some more with it on STATA, and running it again in R (I know this doesn't make sense, but I got used to variable creation in STATA and running models in R), it returned the following error.
Error in `vec_slice()`:
! `x` must be a vector, not `NULL`.
Run `rlang::last_error()` to see where the error occurred.
I know what this means, but I am not super sure how I can troubleshoot it, and the answers already posted here (at least the ones I found) were too specific to the data or too broad to just knowing how to pivot.
I am going to give an example of code below, taken from the first 5 rows of my dataset. The data itself contains many missing values so bear with me.
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"))
I have tried going over the variables again both in STATA and in R to verify if I had made a mistake somewhere, but I've yet to find anything solving this issue. I'm not specifically asking for a solution but even just pointing at the direction of where I might start looking for mistakes will be incredibly useful.
Edit: I thought it would be useful to share that I have a new error being:
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.
You have a column named _merge, that is causing the issue. Rename it or exclude it in col and the pivot will work.
Edit:
In this piece of code, any columns that start with _ like _merge are removed from the frame. Then only those that contain an underscore are used for the pivot.
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"
)