Data not appearing and errors

Bare with me guys, I'm super new to R. I am trying to combine variables and create a new variable and then create a histogram with it, but the histogram keeps returning empty. I believe it may be an issue with my filtering somewhere, but can someone explain where I could've gone wrong?

gss_filtered <- gss_data %>%
  filter(age >= 18 & age <= 29) %>%
  select(class, degree, discpol, polint, mempolit) %>%
  mutate(
    class = trimws(tolower(class)),
    degree = trimws(tolower(degree)),
    discpol = trimws(tolower(discpol)),
    polint = trimws(tolower(polint)),
    mempolit = trimws(tolower(mempolit)),
    
    class = factor(class, levels = c("lower class", "working class", "middle class", "upper class", "no class")),
    degree = factor(degree, levels = c("less than high school", "high school", 
                                       "junior college", "bachelor", "graduate", "no formal degree")),
    discpol = factor(discpol, levels = c("often", "sometimes", "rarely", "never", "unknown")),
    polint = factor(polint, levels = c("very interested", "fairly interested", 
                                       "somewhat interested", "not very interested", "not at all interested", "unknown")),
    mempolit = factor(mempolit, levels = c("yes", "no", "unknown")),
    # Create a composite index for political participation
    political_participation = as.numeric(polint) + as.numeric(discpol) + as.numeric(mempolit)
  )
gss_filtered <- gss_filtered %>%
  filter(
    !is.na(class) & class != "no class",
    !is.na(degree) & degree != "no formal degree",
  )
ggplot(gss_filtered, aes(x = political_participation)) +
  geom_histogram(binwidth = 1, fill = "blue", color = "black", alpha = 0.7) +
  labs(title = "Histogram of Political Participation",
       x = "Political Participation Index", y = "Count") +
  theme_minimal()

Please post the output of

dput(head(gss_filtered, 30))

using the version of gss_filtered that is used in the ggplot() code.

The output is very long but here are the first few lines:

Show in New Window
[[1]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[2]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[3]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[4]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "officer"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[5]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[6]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[7]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[8]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[9]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

[[10]]
[1] "car" "carData" "sjPlot" "htmltools" "officer" "flextable" "kableExtra"
[8] "viridis" "viridisLite" "modelsummary" "fst" "lubridate" "forcats" "stringr"
[15] "dplyr" "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
[22] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

Show in New Window
class degree discpol
0 0 0
polint mempolit political_participation
0 0 0
Show in New Window
structure(list(class = structure(integer(0), levels = c("lower class",
"working class", "middle class", "upper class", "no class"), class = "factor"),
degree = structure(integer(0), levels = c("less than high school",
"high school", "junior college", "bachelor", "graduate",
"no formal degree"), class = "factor"), discpol = structure(integer(0), levels = c("often",
"sometimes", "rarely", "never", "unknown"), class = "factor"),
polint = structure(integer(0), levels = c("very interested",
"fairly interested", "somewhat interested", "not very interested",
"not at all interested", "unknown"), class = "factor"), mempolit = structure(integer(0), levels = c("yes",
"no", "unknown"), class = "factor"), political_participation = numeric(0)), datalabel = "", time.stamp = "15 May 2023 11:19", formats = c("%29.0g",

That is very confusing. The first part of your last post is a list of R packages, repeated 10 times. The last part of your post, the part that starts with structure(, seems to be the output of a dput() call on a data frame with one row filled with zeros.
Let's back up. Please post the output of

dput(head(gss_data, 20))

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.