Starting out with a project

Step 1.

Create a new working directory by starting a new R project. Go to ‘File’, then ‘New Project’, then ‘New Directory’. Provide a name for your directory: e.g., “SOC_252_Project”.

Step 2.

Go to the top again to ‘File’, then new file, then do R markdown and save it as “Participation_3_yourlastname”. Go find your new directory, open it to confirm that you have your .Rproj file, and your .Rmd file.

Step 3.

Setting up the Environment.

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"

Step 4.

Copy-paste the class dataset (wherever it is located on your computer) into your current project directory.

Step 5.

Load class dataset.

ess <- read_fst("All-ESS-Data.fst")

Note: if you get a message like OpenMp was not detected, it is fine. As long as you see the dataset loaded in the top right window, you are golden.

Let’s practice a new skill

Move your mouse near the Run symbol where you see a plus sign with a C. Click on it, then on R.

You will see a a grey bar appear with the following symbol at the top: ```{r}

In there copy the following and run it by clicking the green play button that is at the extreme right of the box:

print(“hell yeah this works!”)

print("hell yeah this works!")
## [1] "hell yeah this works!"

Everything that is in that box is R code. Everything outside is text.

You can test a code chunk by clicking the run button as we just did. If it does not work, it will give you an error code. You cannot “Knit” if there are errors in parts of your code. So make sure to test chunk by chunk. A common error also is to work on your markdown for some time, have referenced objects that are in your environment, but in the markdown sequence are “called upon” before you create the object (or altogether forgot that part). For example, you renamed “France_data” as “df” in your environment but forgot to put that part in your code. When you go to knit, it will not work and it will indicate on what “line” (so where in your code) produced the error.

Practice another skill

Want to see what it looks like?

Add some text here.

Some with two hashtags (for titles)

followed by text

Or just straight typing. Anything you would like.

When you are ready: Go to knit, then click knit to html.

Wait, it will take some time. If it works, it will open a new window and you can click publish and create an account to be able to post html docs online for free.

Participation 3 (2.5%) due Oct. 10

For Participation 3, you must post both your markdown file and an html link to your markdown on the discussion board “Participation 3”.

The following are a series of missions. You DO NOT need to complete them all to receive your marks. The goal is to complete as many as you can and to create the markdown file + html as a fundamental skill moving forward.

Note that some of these will be directly useful to your next stepping stone.

Mission 1

Create tables between ess and variables of interest. Take note of what you need to clean (i.e., the 7, 8, 9, and 77, 88, 99).

varInterest <- ess %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                              netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                              imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt))

happydata <- as.data.frame(table(varInterest$happy))
colnames(happydata) <- c("Happy", "Count")
happydata
##    Happy  Count
## 1      0   3933
## 2      1   3540
## 3      2   7343
## 4      3  13759
## 5      4  17016
## 6      5  52117
## 7      6  43707
## 8      7  87869
## 9      8 130802
## 10     9  76229
## 11    10  51006
netustmdata <- as.data.frame(table(varInterest$netustm))
colnames(netustmdata) <- c("Internet use per minute", "Count")
netustmdata
##     Internet use per minute Count
## 1                         0   351
## 2                         1    18
## 3                         2    18
## 4                         3    18
## 5                         4    12
## 6                         5    95
## 7                         6    69
## 8                         7    28
## 9                         8    75
## 10                        9    29
## 11                       10   444
## 12                       11     5
## 13                       12    12
## 14                       13     2
## 15                       14    10
## 16                       15   772
## 17                       16     4
## 18                       18     8
## 19                       19     1
## 20                       20   689
## 21                       21     1
## 22                       23     1
## 23                       24     2
## 24                       25    45
## 25                       27     1
## 26                       28     3
## 27                       29     8
## 28                       30  5076
## 29                       31     3
## 30                       34     1
## 31                       35    40
## 32                       36     1
## 33                       38     2
## 34                       39     1
## 35                       40   342
## 36                       43     1
## 37                       44     2
## 38                       45  1093
## 39                       46     3
## 40                       47     1
## 41                       48     1
## 42                       49     3
## 43                       50   223
## 44                       55    24
## 45                       58     2
## 46                       59    24
## 47                       60 14889
## 48                       61    39
## 49                       62     8
## 50                       63     8
## 51                       64     2
## 52                       65    67
## 53                       68     6
## 54                       69     8
## 55                       70   194
## 56                       71     3
## 57                       72     2
## 58                       73     1
## 59                       74     2
## 60                       75   320
## 61                       78     1
## 62                       80   308
## 63                       83     1
## 64                       85    26
## 65                       86     1
## 66                       88     2
## 67                       89     2
## 68                       90  5903
## 69                       91     3
## 70                       92     1
## 71                       93     2
## 72                       94     1
## 73                       95    16
## 74                       96     1
## 75                       97     2
## 76                       98     5
## 77                       99     3
## 78                      100   106
## 79                      105   215
## 80                      106     1
## 81                      107     1
## 82                      108     1
## 83                      110   163
## 84                      113     1
## 85                      115     9
## 86                      118     1
## 87                      119    18
## 88                      120 17583
## 89                      121    18
## 90                      122    27
## 91                      123    17
## 92                      124     1
## 93                      125    37
## 94                      126     1
## 95                      127     2
## 96                      128    12
## 97                      129     3
## 98                      130   121
## 99                      131     1
## 100                     132     2
## 101                     133     3
## 102                     135   224
## 103                     138     3
## 104                     140   140
## 105                     141     1
## 106                     143     1
## 107                     145    13
## 108                     147     1
## 109                     149     3
## 110                     150  4073
## 111                     152     1
## 112                     153     2
## 113                     154     1
## 114                     155    10
## 115                     158     8
## 116                     159     1
## 117                     160    72
## 118                     165   107
## 119                     168     2
## 120                     170    89
## 121                     175     5
## 122                     177     1
## 123                     179    17
## 124                     180 12524
## 125                     181    11
## 126                     182     6
## 127                     183    19
## 128                     185    39
## 129                     186     1
## 130                     188     4
## 131                     189     3
## 132                     190    85
## 133                     192     6
## 134                     193     1
## 135                     194     1
## 136                     195   109
## 137                     196     1
## 138                     197     1
## 139                     198     1
## 140                     199     2
## 141                     200   109
## 142                     202     4
## 143                     203     2
## 144                     204     2
## 145                     205    23
## 146                     210  2005
## 147                     213     1
## 148                     214     1
## 149                     215     7
## 150                     216     1
## 151                     218     2
## 152                     219     1
## 153                     220    42
## 154                     222     1
## 155                     225    56
## 156                     227     1
## 157                     230    97
## 158                     235     7
## 159                     237     1
## 160                     238     2
## 161                     239    11
## 162                     240  8565
## 163                     241     4
## 164                     242     3
## 165                     243     7
## 166                     244     8
## 167                     245    16
## 168                     246     2
## 169                     248     2
## 170                     249     1
## 171                     250    42
## 172                     252     1
## 173                     254     1
## 174                     255    51
## 175                     256     1
## 176                     258     1
## 177                     260    68
## 178                     261     2
## 179                     263     1
## 180                     264     3
## 181                     265    12
## 182                     267     1
## 183                     269     1
## 184                     270  1333
## 185                     271     1
## 186                     272     1
## 187                     274     1
## 188                     275     6
## 189                     276     2
## 190                     278     1
## 191                     280    22
## 192                     282     2
## 193                     284     1
## 194                     285    29
## 195                     290    37
## 196                     295     7
## 197                     298     1
## 198                     299     8
## 199                     300  6885
## 200                     301     7
## 201                     302     4
## 202                     303     1
## 203                     304     4
## 204                     305    13
## 205                     306     1
## 206                     308     3
## 207                     310    35
## 208                     311     1
## 209                     312     1
## 210                     313     1
## 211                     315    30
## 212                     316     1
## 213                     320    37
## 214                     321     1
## 215                     325     5
## 216                     327     1
## 217                     328     1
## 218                     330   813
## 219                     333     1
## 220                     335     2
## 221                     337     1
## 222                     338     1
## 223                     340    16
## 224                     342     1
## 225                     343     1
## 226                     345    25
## 227                     346     1
## 228                     350    23
## 229                     352     1
## 230                     355     1
## 231                     358     1
## 232                     359    15
## 233                     360  4718
## 234                     361     4
## 235                     362     3
## 236                     363     4
## 237                     365    11
## 238                     366     7
## 239                     368     2
## 240                     370    15
## 241                     372     3
## 242                     375    20
## 243                     377     2
## 244                     380    24
## 245                     385     5
## 246                     390   502
## 247                     395     2
## 248                     397     1
## 249                     398     1
## 250                     400    11
## 251                     403     1
## 252                     405    16
## 253                     410    17
## 254                     415     1
## 255                     416     1
## 256                     419     8
## 257                     420  1812
## 258                     421     2
## 259                     422     1
## 260                     423     1
## 261                     425     5
## 262                     427     3
## 263                     430     8
## 264                     433     1
## 265                     435     8
## 266                     440     7
## 267                     445     3
## 268                     450   225
## 269                     455     2
## 270                     458     1
## 271                     460     7
## 272                     465     7
## 273                     470     8
## 274                     477     1
## 275                     479     6
## 276                     480  4791
## 277                     481     1
## 278                     482     2
## 279                     483     1
## 280                     484     1
## 281                     485     3
## 282                     486     1
## 283                     487     1
## 284                     488    11
## 285                     489     2
## 286                     490    18
## 287                     492     1
## 288                     494     1
## 289                     495    18
## 290                     500    13
## 291                     505     2
## 292                     507     1
## 293                     510   382
## 294                     520     8
## 295                     522     1
## 296                     525    15
## 297                     527     1
## 298                     528     1
## 299                     530    16
## 300                     533     2
## 301                     535     1
## 302                     538     1
## 303                     539    13
## 304                     540  1504
## 305                     542     1
## 306                     545     4
## 307                     549     1
## 308                     550     3
## 309                     555     4
## 310                     556     1
## 311                     560     7
## 312                     565     1
## 313                     570   168
## 314                     575     1
## 315                     580     3
## 316                     585     7
## 317                     590    14
## 318                     595     6
## 319                     599     3
## 320                     600  4342
## 321                     601     4
## 322                     602     4
## 323                     603     3
## 324                     604     2
## 325                     605     3
## 326                     608     1
## 327                     609     2
## 328                     610    12
## 329                     611     2
## 330                     612     1
## 331                     614     1
## 332                     615     9
## 333                     620     6
## 334                     625     1
## 335                     630   147
## 336                     631     1
## 337                     635     1
## 338                     637     1
## 339                     640     9
## 340                     642     1
## 341                     645     4
## 342                     647     1
## 343                     650    13
## 344                     653     1
## 345                     655     2
## 346                     659     9
## 347                     660   353
## 348                     662     1
## 349                     665     1
## 350                     670     1
## 351                     671     1
## 352                     675     1
## 353                     680     1
## 354                     690    21
## 355                     693     1
## 356                     696     1
## 357                     699     1
## 358                     710     2
## 359                     719     1
## 360                     720  1786
## 361                     721     1
## 362                     722     1
## 363                     725     2
## 364                     727     2
## 365                     730     3
## 366                     732     1
## 367                     735     2
## 368                     740     3
## 369                     745     1
## 370                     749     1
## 371                     750    48
## 372                     754     1
## 373                     760     2
## 374                     761     1
## 375                     765     4
## 376                     770     2
## 377                     775     1
## 378                     777     2
## 379                     778     1
## 380                     780   137
## 381                     790     1
## 382                     794     1
## 383                     800     1
## 384                     810    13
## 385                     813     1
## 386                     818     1
## 387                     820     1
## 388                     828     1
## 389                     831     1
## 390                     840   286
## 391                     841     1
## 392                     847     1
## 393                     848     1
## 394                     850     1
## 395                     860     1
## 396                     870    11
## 397                     873     1
## 398                     880     2
## 399                     890     2
## 400                     895     1
## 401                     899     4
## 402                     900   307
## 403                     901     2
## 404                     902     1
## 405                     915     3
## 406                     920     3
## 407                     925     1
## 408                     930    10
## 409                     939     1
## 410                     940     2
## 411                     950     3
## 412                     957     1
## 413                     959     2
## 414                     960   207
## 415                     970     1
## 416                     972     1
## 417                     975     2
## 418                     976     1
## 419                     980     2
## 420                     988     1
## 421                     990     5
## 422                    1000     1
## 423                    1010     1
## 424                    1020    35
## 425                    1038     1
## 426                    1050     2
## 427                    1060     1
## 428                    1078     1
## 429                    1080    76
## 430                    1115     2
## 431                    1119     1
## 432                    1140     9
## 433                    1141     1
## 434                    1145     1
## 435                    1150     1
## 436                    1167     1
## 437                    1170     3
## 438                    1179     1
## 439                    1196     1
## 440                    1200    59
## 441                    1205     4
## 442                    1220     2
## 443                    1230     2
## 444                    1231     1
## 445                    1232     1
## 446                    1234     1
## 447                    1240     2
## 448                    1250     1
## 449                    1260     1
## 450                    1284     1
## 451                    1320     6
## 452                    1343     1
## 453                    1380     7
## 454                    1410    11
## 455                    1430     1
## 456                    1439     1
## 457                    1440    70
imuecltdata <- as.data.frame(table(varInterest$imueclt))
colnames(imuecltdata) <- c("Culture life", "Count")
imuecltdata
##    Culture life Count
## 1             0 24444
## 2             1 16412
## 3             2 27895
## 4             3 38166
## 5             4 37350
## 6             5 94865
## 7             6 49916
## 8             7 66002
## 9             8 60620
## 10            9 23387
## 11           10 29171

Mission 2

Look into an additional socio-demographic variable (other than the 5 covered in the tutorial), create a table and take note of how to clean it. You can do more than one, but do at least one for the mission.

Provide the ess data portal link here. For example, like so:

https://ess-search.nsd.no/en/variable/32558258-72b1-479b-8cca-49c9d569408d

Take note of the categories and consider how to recode them (using mutate) so as to create fewer categories (if needed) that are meaningful for analytical purposes. For example, for the one above you can consider recoding 1, 2, and 5 together, 3 and 4 together, and 6 separately.

mission2 <- ess %>% mutate(ifredu = ifelse(ifredu %in% c(55, 77, 88, 99), NA, ifredu))

edudata <- as.data.frame(table(mission2$ifredu))
colnames(edudata) <- c("Compared to other people in [country], I have had a fair chance of achieving the level of education I was seeking.", "Count")
edudata
##    Compared to other people in [country], I have had a fair chance of achieving the level of education I was seeking.
## 1                                                                                                                   0
## 2                                                                                                                   1
## 3                                                                                                                   2
## 4                                                                                                                   3
## 5                                                                                                                   4
## 6                                                                                                                   5
## 7                                                                                                                   6
## 8                                                                                                                   7
## 9                                                                                                                   8
## 10                                                                                                                  9
## 11                                                                                                                 10
##    Count
## 1   2032
## 2    740
## 3   1516
## 4   2163
## 5   1956
## 6   4627
## 7   3229
## 8   5109
## 9   7914
## 10  5509
## 11 12687

Mission 3

Filter to your country of interest and use mutate to clean your variables. If you want, you can “select” all your variables of interest now, including the socio-demographic ones.

Note: whenever you recode or clean, always double check what you did by comparing pre and post. So, for example, if you tried to clean and named your dataset france_clean from the uncleaned france_data, do tables of variables of interest between the two to see if the recodes and cleaning worked as intended.

france_data <- ess[ess$cntry == "FR", ]
mission3 <- france_data %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                                   netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                                   imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt)) %>%
  select(cntry, happy, netustm, imueclt)

Mission 4

Do a datasummary_skim of variables of interest. You can select variables as follows:

# datasummary_skim(dataset %>% select(v1, v2, v3))
datasummary_skim(france_data %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                                   netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                                   imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt)) %>% 
                   select(cntry, happy, netustm, imueclt))
Unique (#) Missing (%) Mean SD Min Median Max
happy 12 0 7.2 1.8 0.0 8.0 10.0
netustm 80 76 189.7 170.1 0.0 120.0 1380.0
imueclt 12 1 5.3 2.7 0.0 5.0 10.0

Note: code here and below, you just need to replace with what you need and remove the hashtags.

Mission 5

Do a quick frequency check for socio-demographics of interest, then visualize. Here’s an example for geo:

mission5 <- france_data %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                                   netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                                   imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt)) %>% 
                   select(cntry, happy, netustm, imueclt)

mission5 <- mission5 %>% mutate(happycut = cut(happy, breaks = c(0, 1, 3, 6, 9, 10), 
                                               labels = c("Extremely Unhappy", 
                                                          "Unhappy", 
                                                          "Moderately Happy", 
                                                          "Happy", 
                                                          "Extremely Happy"), include.lowest = TRUE))

freq(mission5$happycut)
##                       n    % val%
## Extremely Unhappy   147  0.8  0.8
## Unhappy             531  2.8  2.8
## Moderately Happy   4732 24.9 24.9
## Happy             11980 62.9 63.0
## Extremely Happy    1622  8.5  8.5
## NA                   26  0.1   NA
mission5 %>%
  drop_na(happycut) %>%
  select(happycut) %>%
  freq() %>%
  as.data.frame() %>%
  ggplot(aes(x=factor(rownames(.),
                     levels= c("Extremely Unhappy", 
                              "Unhappy", 
                              "Moderately Happy", 
                              "Happy", 
                              "Extremely Happy")), 
             y=`%`)) +
  geom_col() +
  labs(title = "Distribution of Happiness",
       x = "Happiness")

Depending on what you look into, you will note that some of the percentage seem off relative to what it would be for the entire population. For example, % rural in the sample vs. % rural in the population. That is why we eventually need to make adjustments and apply survey weights. We will talk more about the specific survey design of the ESS in future lectures.

So let’s turn to do conditional probabilities, giving us a better sense of what’s going conditional on a specific category (i.e., dividing by say where someone lives rather than by the total).

Mission 6

Do column percentages (as conditional %) with cprop(). Here’s an example:

mission6 <- france_data %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                                   netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                                   imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt)) %>% 
                   select(cntry, happy, netustm, imueclt)
table(mission6$happy, mission6$imueclt) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   0       2.4   0.6   0.5   0.3   0.2   0.4   0.1   0.1   0.4   0.2   0.8   0.5
##   1       0.6   1.2   0.4   0.3   0.2   0.4   0.1   0.2   0.2   0.0   0.2   0.3
##   2       2.4   3.5   1.6   0.7   1.0   1.0   0.4   0.6   0.4   0.5   0.8   1.0
##   3       4.1   3.2   3.1   2.3   1.9   1.8   1.3   0.9   1.1   0.9   1.4   1.8
##   4       3.4   5.4   4.8   3.7   3.0   2.3   2.7   2.0   1.6   1.2   2.2   2.7
##   5      22.3  19.5  17.8  15.4  14.1  14.0   8.2   7.8   7.5   8.4   9.5  12.3
##   6       6.7  12.2  11.0  12.9  13.5  10.3  11.3   9.1   7.4   7.9   5.7   9.8
##   7      11.9  15.7  18.6  20.7  22.0  22.8  27.0  26.0  19.9  16.9  15.8  21.1
##   8      19.2  18.9  22.2  28.7  28.0  28.2  32.5  35.5  36.6  30.5  27.7  29.5
##   9      11.2  11.8  12.1   8.6  11.1  10.3  11.1  12.6  16.4  24.2  15.0  12.6
##   10     15.8   8.0   7.9   6.5   5.0   8.7   5.3   5.1   8.4   9.3  20.7   8.5
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0

You can also do row percentages with rprop().

Mission 7

Do cross tabs (especially useful for exploring relationships between categorical variables). Here’s an example we saw:

mission7 <- france_data %>% mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
                                   netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
                                   imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt)) %>% 
                   select(cntry, happy, netustm, imueclt)

happyimueclt <- datasummary_crosstab(happy ~ imueclt, data = mission7)
happyimueclt
happy 0 1  2  3  4  5  6  7  8  9  10 All
0 N 29 4 7 5 3 14 2 3 10 2 10 90
% row 32.2 4.4 7.8 5.6 3.3 15.6 2.2 3.3 11.1 2.2 11.1 100.0
1 N 7 8 5 4 3 13 3 5 5 0 2 57
% row 12.3 14.0 8.8 7.0 5.3 22.8 5.3 8.8 8.8 0.0 3.5 100.0
2 N 29 23 21 11 15 36 9 14 9 4 10 185
% row 15.7 12.4 11.4 5.9 8.1 19.5 4.9 7.6 4.9 2.2 5.4 100.0
3 N 49 21 41 35 29 64 27 23 26 8 17 346
% row 14.2 6.1 11.8 10.1 8.4 18.5 7.8 6.6 7.5 2.3 4.9 100.0
4 N 41 36 63 56 47 83 55 49 39 10 26 514
% row 8.0 7.0 12.3 10.9 9.1 16.1 10.7 9.5 7.6 1.9 5.1 100.0
5 N 268 129 234 233 217 508 169 191 179 72 112 2349
% row 11.4 5.5 10.0 9.9 9.2 21.6 7.2 8.1 7.6 3.1 4.8 100.0
6 N 80 81 144 195 208 373 234 223 176 68 68 1869
% row 4.3 4.3 7.7 10.4 11.1 20.0 12.5 11.9 9.4 3.6 3.6 100.0
7 N 143 104 244 314 339 831 557 637 472 145 187 4003
% row 3.6 2.6 6.1 7.8 8.5 20.8 13.9 15.9 11.8 3.6 4.7 100.0
8 N 230 125 291 435 432 1024 671 871 870 262 328 5591
% row 4.1 2.2 5.2 7.8 7.7 18.3 12.0 15.6 15.6 4.7 5.9 100.0
9 N 135 78 159 131 171 375 230 309 391 208 178 2386
% row 5.7 3.3 6.7 5.5 7.2 15.7 9.6 13.0 16.4 8.7 7.5 100.0
10 N 189 53 104 98 77 316 109 126 200 80 245 1622
% row 11.7 3.3 6.4 6.0 4.7 19.5 6.7 7.8 12.3 4.9 15.1 100.0
All N 1203 662 1315 1517 1545 3643 2066 2452 2380 860 1186 19038
% row 6.3 3.5 6.9 8.0 8.1 19.1 10.9 12.9 12.5 4.5 6.2 100.0

Note: you can do it for a continuous/discrete variable, but the table will yield column percentages for each of the values for a column (for example, for stfdem, all of 0 to 10).