Coding Tutorial: Grouping, Filtering, Mutating, Selecting, and Plotting

load("~/Dropbox/PSCI2075_2025/PSCI_2075_v2.1.RData")
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
nes |> 
    summary()
               follow       birthyr                     turnout12  
 Most of the time :610   Min.   :1921   Definitely voted     :912  
 Some of the Time :349   1st Qu.:1955   Probably did not vote:218  
 Only now and then:157   Median :1968   Not completely sure  : 48  
 Hardly at all    : 62   Mean   :1968                              
                         3rd Qu.:1982                              
                         Max.   :1997                              
                                                                   
          vote12                   meet    
 Mitt Romney :364   Extremely Likely :117  
 Barack Obama:505   Very Likely      :127  
 Someone Else: 68   Moderately likely:232  
 Not asked   :241   A little likely  :247  
                    Not at all likely:455  
                                           
                                           
                                march     
 Have done this in past 4 years    : 172  
 Have not done this in past 4 years:1006  
                                          
                                          
                                          
                                          
                                          
                           compromise     ftobama          ftblack      
 Compromies to get things done  :673   Min.   :  0.00   Min.   :  0.00  
 Sticks to his or her principles:505   1st Qu.:  5.00   1st Qu.: 50.00  
                                       Median : 53.50   Median : 70.00  
                                       Mean   : 48.76   Mean   : 66.97  
                                       3rd Qu.: 87.00   3rd Qu.: 89.00  
                                       Max.   :100.00   Max.   :100.00  
                                                                        
    ftwhite          fthisp           ftgay           fttrump      
 Min.   :  0.0   Min.   :  0.00   Min.   :  0.00   Min.   :  0.00  
 1st Qu.: 51.0   1st Qu.: 50.00   1st Qu.: 40.00   1st Qu.:  2.00  
 Median : 73.0   Median : 68.00   Median : 54.00   Median : 30.00  
 Mean   : 71.1   Mean   : 65.65   Mean   : 57.39   Mean   : 38.26  
 3rd Qu.: 90.0   3rd Qu.: 88.00   3rd Qu.: 88.00   3rd Qu.: 72.00  
 Max.   :100.0   Max.   :100.00   Max.   :100.00   Max.   :100.00  
                                                                   
     fthrc          ftsanders        ftpolice          ftfem       
 Min.   :  0.00   Min.   :  0.0   Min.   :  0.00   Min.   :  0.00  
 1st Qu.:  3.00   1st Qu.: 19.0   1st Qu.: 50.00   1st Qu.: 29.00  
 Median : 45.50   Median : 51.0   Median : 70.00   Median : 50.00  
 Mean   : 43.11   Mean   : 50.5   Mean   : 66.22   Mean   : 51.55  
 3rd Qu.: 76.00   3rd Qu.: 81.0   3rd Qu.: 90.00   3rd Qu.: 76.00  
 Max.   :100.00   Max.   :100.0   Max.   :100.00   Max.   :100.00  
                                                                   
    ftmuslim          ftsci        econnow
 Min.   :  0.00   Min.   :  0.00   1: 97  
 1st Qu.: 18.00   1st Qu.: 55.25   2:287  
 Median : 50.00   Median : 80.00   3:406  
 Mean   : 45.44   Mean   : 72.92   4:246  
 3rd Qu.: 69.00   3rd Qu.: 92.00   5:142  
 Max.   :100.00   Max.   :100.00          
                                          
                              lcself                  disc_b   
 Neither liberal nor conservative:327   A great deal     :218  
 Very liberal                    :173   A lot            :288  
 Somewhat liberal                :160   A moderate amount:356  
 Somewhat conservative           :158   A little         :247  
 Very conservative               :140   None at all      : 67  
 Closer to conservatives         :123   Skipped          :  2  
 (Other)                         : 97                          
               disc_h                  disc_g                  disc_w   
 A great deal     :103   A great deal     :173   A great deal     : 75  
 A lot            :250   A lot            :293   A lot            :114  
 A moderate amount:440   A moderate amount:371   A moderate amount:232  
 A little         :300   A little         :246   A little         :384  
 None at all      : 84   None at all      : 93   None at all      :372  
 Skipped          :  1   Skipped          :  2   Skipped          :  1  
                                                                        
               disc_m                               disc_fed  
 A great deal     : 53   Treats both the same           :353  
 A lot            : 78   Treats whites much better      :199  
 A moderate amount:202   Treats whites a little better  :162  
 A little         :358   Treats blacks a little better  :152  
 None at all      :486   Treats whites moderately better:138  
 Skipped          :  1   Treats blacks moderately better: 87  
                         (Other)                        : 87  
                          disc_police                        immig_numb 
 Treats both the same           :410   Increased a lot            : 83  
 Treats whites much better      :337   Increased a moderate amount:129  
 Treats whites a little better  :221   Increased a little         :145  
 Treats whites moderately better:181   Kept the same              :444  
 Treats blacks a little better  : 17   Decreased a little         :147  
 Treats blacks moderately better:  8   Decreased a moderate amount:108  
 (Other)                        :  4   Decreased a lot            :122  
             terror_worry                healthspend 
 Extremely worried :177   Increase a great deal:259  
 Very worried      :159   Incrase moderately   :237  
 Moderately worried:190   No Change            :236  
 Slightly worried  :149   Increase a little    :194  
 Not at all worried: 96   Decrease a great deal:132  
 Not asked         :407   Decrease a little    : 66  
                          (Other)              : 54  
                     finwell   
 A little worse          :243  
 A little better         :223  
 Moderately worse        :213  
 A great deal worse      :200  
 The same as 20 years ago:150  
 Moderately better       :105  
 (Other)                 : 44  
                                              warmcause  
 Mostly by human activity                          :499  
 Mostly by natural causes                          :287  
 About equally by human activity and natural causes:392  
                                                         
                                                         
                                                         
                                                         
                    freetrade            stopblack            stop_ever  
 Favor a great deal      : 98   Never         : 41   Has happened  :350  
 Favor moderately        :139   Rarely        : 75   Never happened:239  
 Favor a little          :183   Sometimes     :427   Skipped       :  1  
 Neither favor nor oppose:438   Somewhat often:331   Not asked     :588  
 Oppose a little         :108   Very often    :304                       
 Oppose moderately       :106                                            
 Opposed a great deal    :106                                            
                  birthright_b        bo_muslim                  amer_ident 
 Not asked              :576   Muslim      :391   Extremely important :506  
 Oppose a great deal    :161   Not a Muslim:781   Very important      :271  
 Neither favor or oppose:115   Skipped     :  6   Moderately important:213  
 Favor a great deal     : 92                      A little important  :109  
 Favor moderately       : 76                      Not important at all: 79  
 Oppose a little        : 61                                                
 (Other)                : 97                                                
                 marstat                     employ   
 Married             :560   Full-time           :466  
 Separated           : 27   Retired             :194  
 Divorced            :123   Part-time           :141  
 Widowed             : 60   Permanently disabled:107  
 Single              :352   Unemployed          :101  
 Domestic Partnership: 56   Homemaker           : 91  
                            (Other)             : 78  
                   educ        gender          race             pid3    
 No HS               :100   Male  :567   White   :863   Democrat  :449  
 High school graduate:402   Female:611   Black   :131   Republican:276  
 Some college        :252                Hispanic:110   Indepent  :375  
 2-year              :105                Mixed   : 29   Other     : 74  
 4-year              :198                Asian   : 22   Not sure  :  4  
 Post-grad           :121                Other   : 15                   
                                         (Other) :  8                   
                pid7                   ideo5                    pew_churatd 
 Strong Democrat  :295   Very liberal     :141   Never                :376  
 Independent      :201   Liberal          :207   Seldom               :267  
 Strong Republican:157   Moderate         :367   Once a week          :179  
 Weak Democrat    :141   Conservative     :257   A few times a year   :145  
 Weak Republican  :114   Very conservative:119   More than once a week: 95  
 (Other)          :256   Not sure         : 87   Once or twice a month: 93  
 NA's             : 14                           (Other)              : 23  
     faminc          weight                                dpolice.new 
 Min.   : 1.00   Min.   :0.1693   Treats whites much better      :337  
 1st Qu.: 3.00   1st Qu.:0.3948   Treats whites moderately better:181  
 Median : 6.00   Median :0.8105   Treats whites a little better  :221  
 Mean   :16.76   Mean   :0.9941   Treats both the same           :410  
 3rd Qu.: 9.00   3rd Qu.:1.2210   NA's                           : 29  
 Max.   :98.00   Max.   :7.0104                                        
                                                                       
nes |> 
    summarize(mean_ftobama = mean(ftobama))
# A tibble: 1 × 1
  mean_ftobama
         <dbl>
1         48.8
nes |> 
    group_by(vote12) |>
    summarize(mean_ftobama = mean(ftobama))
# A tibble: 4 × 2
  vote12       mean_ftobama
  <fct>               <dbl>
1 Mitt Romney          11.8
2 Barack Obama         77.8
3 Someone Else         23.9
4 Not asked            50.7
nes |> 
    filter(vote12 == "Barack Obama") |>
    summarize(mean_ftobama = mean(ftobama))
# A tibble: 1 × 1
  mean_ftobama
         <dbl>
1         77.8
nes |> 
    mutate(new_var = "this is a new variable") |> 
    glimpse()
Rows: 1,178
Columns: 52
$ follow       <fct> Most of the time, Some of the Time, Most of the time, Mos…
$ birthyr      <int> 1960, 1957, 1963, 1980, 1974, 1958, 1978, 1951, 1973, 193…
$ turnout12    <fct> Definitely voted, Probably did not vote, Definitely voted…
$ vote12       <fct> Barack Obama, Not asked, Mitt Romney, Barack Obama, Mitt …
$ meet         <fct> Extremely Likely, A little likely, Extremely Likely, Not …
$ march        <fct> Have done this in past 4 years, Have not done this in pas…
$ compromise   <fct> Compromies to get things done, Compromies to get things d…
$ ftobama      <int> 100, 39, 1, 89, 1, 0, 73, 0, 12, 87, 32, 10, 70, 80, 4, 0…
$ ftblack      <int> 100, 6, 50, 61, 61, 50, 100, 70, 50, 75, 53, 80, 88, 67, …
$ ftwhite      <int> 100, 74, 50, 64, 58, 51, 70, 70, 50, 90, 52, 80, 90, 74, …
$ fthisp       <int> 100, 6, 50, 61, 71, 51, 100, 69, 50, 51, 54, 80, 90, 57, …
$ ftgay        <int> 96, 75, 16, 62, 55, 46, 100, 49, 5, 85, 52, 70, 86, 27, 9…
$ fttrump      <int> 1, 28, 100, 0, 13, 61, 5, 85, 70, 5, 74, 95, 39, 82, 91, …
$ fthrc        <int> 76, 52, 1, 69, 1, 1, 60, 0, 3, 87, 6, 10, 49, 3, 1, 0, 79…
$ ftsanders    <int> 84, 13, 2, 71, 13, 11, 100, 40, 31, 97, 51, 10, 90, 41, 1…
$ ftpolice     <int> 51, 65, 85, 56, 89, 55, 34, 85, 80, 60, 97, 90, 91, 99, 9…
$ ftfem        <int> 62, 53, 2, 60, 31, 0, 100, 45, 21, 61, 34, 50, 80, 22, 54…
$ ftmuslim     <int> 20, 22, 5, 61, 22, 11, 100, 40, 12, 72, 43, 50, 81, 53, 2…
$ ftsci        <int> 100, 91, 61, 96, 40, 71, 100, 76, 50, 91, 98, 90, 91, 1, …
$ econnow      <fct> 2, 4, 5, 2, 4, 4, 2, 3, 4, 2, 4, 3, 2, 5, 4, 4, 2, 3, 4, …
$ lcself       <fct> Neither liberal nor conservative, Neither liberal nor con…
$ disc_b       <fct> A little, None at all, None at all, A moderate amount, A …
$ disc_h       <fct> A little, A moderate amount, None at all, A little, A lit…
$ disc_g       <fct> A moderate amount, A moderate amount, None at all, A mode…
$ disc_w       <fct> A little, A lot, A lot, None at all, A great deal, A litt…
$ disc_m       <fct> A little, A little, A lot, None at all, A great deal, Non…
$ disc_fed     <fct> Treats whites a little better, Treats whites a little bet…
$ disc_police  <fct> Treats whites moderately better, Treats whites a little b…
$ immig_numb   <fct> Increased a moderate amount, Increased a little, Decrease…
$ terror_worry <fct> Not at all worried, Not asked, Extremely worried, Not at …
$ healthspend  <fct> Increase a great deal, Incrase moderately, Decrease moder…
$ finwell      <fct> Moderately worse, A little worse, A great deal worse, Mod…
$ warmcause    <fct> Mostly by human activity, About equally by human activity…
$ freetrade    <fct> Neither favor nor oppose, Neither favor nor oppose, Neith…
$ stopblack    <fct> Somewhat often, Somewhat often, Sometimes, Somewhat often…
$ stop_ever    <fct> Has happened, Not asked, Not asked, Not asked, Has happen…
$ birthright_b <fct> Favor moderately, Not asked, Not asked, Not asked, Oppose…
$ bo_muslim    <fct> Not a Muslim, Not a Muslim, Muslim, Not a Muslim, Not a M…
$ amer_ident   <fct> A little important, Moderately important, Extremely impor…
$ marstat      <fct> Married, Divorced, Single, Married, Married, Married, Sin…
$ employ       <fct> Full-time, Full-time, Unemployed, Full-time, Full-time, F…
$ educ         <fct> 4-year, Post-grad, 4-year, 4-year, High school graduate, …
$ gender       <fct> Male, Female, Male, Male, Male, Male, Male, Male, Male, M…
$ race         <fct> White, White, White, White, White, White, White, White, W…
$ pid3         <fct> Democrat, Indepent, Republican, Democrat, Other, Indepent…
$ pid7         <fct> Strong Democrat, Independent, Weak Republican, Strong Dem…
$ ideo5        <fct> Not sure, Liberal, Conservative, Liberal, Conservative, C…
$ pew_churatd  <fct> Never, A few times a year, Never, Never, Once a week, Onc…
$ faminc       <int> 4, 8, 1, 12, 10, 7, 97, 10, 8, 10, 11, 5, 5, 6, 8, 97, 97…
$ weight       <dbl> 0.9511600, 2.6701962, 1.4303896, 0.9139662, 0.2639346, 1.…
$ dpolice.new  <fct> Treats whites moderately better, Treats whites a little b…
$ new_var      <chr> "this is a new variable", "this is a new variable", "this…
nes |> 
    mutate(ftobama_cat = case_when(ftobama >= 50 ~ "Generally Positive",
                                    ftobama < 50 ~ "Generally Negative")) |> 
    glimpse()
Rows: 1,178
Columns: 52
$ follow       <fct> Most of the time, Some of the Time, Most of the time, Mos…
$ birthyr      <int> 1960, 1957, 1963, 1980, 1974, 1958, 1978, 1951, 1973, 193…
$ turnout12    <fct> Definitely voted, Probably did not vote, Definitely voted…
$ vote12       <fct> Barack Obama, Not asked, Mitt Romney, Barack Obama, Mitt …
$ meet         <fct> Extremely Likely, A little likely, Extremely Likely, Not …
$ march        <fct> Have done this in past 4 years, Have not done this in pas…
$ compromise   <fct> Compromies to get things done, Compromies to get things d…
$ ftobama      <int> 100, 39, 1, 89, 1, 0, 73, 0, 12, 87, 32, 10, 70, 80, 4, 0…
$ ftblack      <int> 100, 6, 50, 61, 61, 50, 100, 70, 50, 75, 53, 80, 88, 67, …
$ ftwhite      <int> 100, 74, 50, 64, 58, 51, 70, 70, 50, 90, 52, 80, 90, 74, …
$ fthisp       <int> 100, 6, 50, 61, 71, 51, 100, 69, 50, 51, 54, 80, 90, 57, …
$ ftgay        <int> 96, 75, 16, 62, 55, 46, 100, 49, 5, 85, 52, 70, 86, 27, 9…
$ fttrump      <int> 1, 28, 100, 0, 13, 61, 5, 85, 70, 5, 74, 95, 39, 82, 91, …
$ fthrc        <int> 76, 52, 1, 69, 1, 1, 60, 0, 3, 87, 6, 10, 49, 3, 1, 0, 79…
$ ftsanders    <int> 84, 13, 2, 71, 13, 11, 100, 40, 31, 97, 51, 10, 90, 41, 1…
$ ftpolice     <int> 51, 65, 85, 56, 89, 55, 34, 85, 80, 60, 97, 90, 91, 99, 9…
$ ftfem        <int> 62, 53, 2, 60, 31, 0, 100, 45, 21, 61, 34, 50, 80, 22, 54…
$ ftmuslim     <int> 20, 22, 5, 61, 22, 11, 100, 40, 12, 72, 43, 50, 81, 53, 2…
$ ftsci        <int> 100, 91, 61, 96, 40, 71, 100, 76, 50, 91, 98, 90, 91, 1, …
$ econnow      <fct> 2, 4, 5, 2, 4, 4, 2, 3, 4, 2, 4, 3, 2, 5, 4, 4, 2, 3, 4, …
$ lcself       <fct> Neither liberal nor conservative, Neither liberal nor con…
$ disc_b       <fct> A little, None at all, None at all, A moderate amount, A …
$ disc_h       <fct> A little, A moderate amount, None at all, A little, A lit…
$ disc_g       <fct> A moderate amount, A moderate amount, None at all, A mode…
$ disc_w       <fct> A little, A lot, A lot, None at all, A great deal, A litt…
$ disc_m       <fct> A little, A little, A lot, None at all, A great deal, Non…
$ disc_fed     <fct> Treats whites a little better, Treats whites a little bet…
$ disc_police  <fct> Treats whites moderately better, Treats whites a little b…
$ immig_numb   <fct> Increased a moderate amount, Increased a little, Decrease…
$ terror_worry <fct> Not at all worried, Not asked, Extremely worried, Not at …
$ healthspend  <fct> Increase a great deal, Incrase moderately, Decrease moder…
$ finwell      <fct> Moderately worse, A little worse, A great deal worse, Mod…
$ warmcause    <fct> Mostly by human activity, About equally by human activity…
$ freetrade    <fct> Neither favor nor oppose, Neither favor nor oppose, Neith…
$ stopblack    <fct> Somewhat often, Somewhat often, Sometimes, Somewhat often…
$ stop_ever    <fct> Has happened, Not asked, Not asked, Not asked, Has happen…
$ birthright_b <fct> Favor moderately, Not asked, Not asked, Not asked, Oppose…
$ bo_muslim    <fct> Not a Muslim, Not a Muslim, Muslim, Not a Muslim, Not a M…
$ amer_ident   <fct> A little important, Moderately important, Extremely impor…
$ marstat      <fct> Married, Divorced, Single, Married, Married, Married, Sin…
$ employ       <fct> Full-time, Full-time, Unemployed, Full-time, Full-time, F…
$ educ         <fct> 4-year, Post-grad, 4-year, 4-year, High school graduate, …
$ gender       <fct> Male, Female, Male, Male, Male, Male, Male, Male, Male, M…
$ race         <fct> White, White, White, White, White, White, White, White, W…
$ pid3         <fct> Democrat, Indepent, Republican, Democrat, Other, Indepent…
$ pid7         <fct> Strong Democrat, Independent, Weak Republican, Strong Dem…
$ ideo5        <fct> Not sure, Liberal, Conservative, Liberal, Conservative, C…
$ pew_churatd  <fct> Never, A few times a year, Never, Never, Once a week, Onc…
$ faminc       <int> 4, 8, 1, 12, 10, 7, 97, 10, 8, 10, 11, 5, 5, 6, 8, 97, 97…
$ weight       <dbl> 0.9511600, 2.6701962, 1.4303896, 0.9139662, 0.2639346, 1.…
$ dpolice.new  <fct> Treats whites moderately better, Treats whites a little b…
$ ftobama_cat  <chr> "Generally Positive", "Generally Negative", "Generally Ne…
nes <- nes |> 
    mutate(ftobama_cat = case_when(ftobama >= 50 ~ "Generally Positive",
                                    ftobama < 50 ~ "Generally Negative"))

g <- nes |> 
    ggplot(aes(x = ftobama_cat)) + 
    geom_bar() 

g + 
    theme_minimal() + 
    labs(
        x = "Feelings toward Obama (Categorical)",
        y = "Frequency", 
        title = "Frequency of NES respondents who are generally negative\n vs. generally positive toward Obama"
    )