R programming for beginners (GV900)

Lesson 2: Data table ~ filter rows

Saturday, December 23, 2023

Video of Lesson 2 ~ Part 1: R script, R Markdown & Quarto

1 R Script, R Markdown & Quarto

1.1 Why use R Markdown

  • Reproducibility: R Markdown allow you to document your analysis step-by-step. If you need to rerun or modify your analysis in the future, having an R script makes it easier to reproduce the same results without relying on manual commands entered in the console.

  • Automation: Markdown enable automation of repetitive tasks. You can create functions, loops, and conditional statements in Markdown to automate data processing, analysis, or visualization procedures.

  • Organization: Markdown help organize your code logically. You can segment code into sections, add comments, and use proper formatting, making it easier to read and understand, especially for larger or complex analyses.

  • Collaboration: Sharing R Markdown with collaborators or colleagues facilitates collaboration. It allows others to replicate your analysis, understand your workflow, and make contributions or modifications if needed.

  • Version Control: Storing Markdown in version control systems (e.g., Git) allows you to track changes made to the code, revert to previous versions if necessary, and collaborate efficiently in a team setting.

  • Debugging: When errors occur, having the code in a script format allows for easier debugging. You can identify and fix issues more effectively by pinpointing the exact line or section of code causing the problem.

  • Documentation: Markdown serve as documentation for your analysis workflow. You can include comments, explanations, and annotations within the script to clarify each step of your analysis.

1.2 Quarto tutorials

  • Welcome to Quarto Workshop! | Led by Tom Mock

  • Hello, Quarto: A World of Possibilities | Led by Mine Cetinkaya-Rundel

  • Get started with Quarto | Mine Çetinkaya-Rundel

  • Quarto for Academics | Mine Çetinkaya-Rundel

Lesson 2 ~ Part 2: Filtering Dataset Rows

2 Install and library R packages

Code
# install.packages("tidyverse")
# install.packages("carData")

library(tidyverse)

library(carData)

3 Load data

Code
data(BEPS)# BEPS = British Election Panel Study (https://rdrr.io/cran/carData/man/BEPS.html)
 
# View(BEPS)# shows us the dataset in a new window
#?BEPS
#(for help file) gives me the codebook
# What is each observation/row?
names(BEPS) #shows us the column names for BEPS
 [1] "vote"                    "age"                    
 [3] "economic.cond.national"  "economic.cond.household"
 [5] "Blair"                   "Hague"                  
 [7] "Kennedy"                 "Europe"                 
 [9] "political.knowledge"     "gender"                 

4 Rational operators

  • greater than: >
Code
BEPS |> 
  filter(age > 75) |> 
  head(10)
               vote age economic.cond.national economic.cond.household Blair
8            Labour  77                      3                       4     4
15           Labour  77                      2                       3     2
19           Labour  79                      3                       3     4
35     Conservative  76                      3                       4     4
65           Labour  77                      3                       3     4
73     Conservative  88                      3                       3     4
80           Labour  76                      4                       3     4
83     Conservative  78                      3                       3     4
90 Liberal Democrat  82                      4                       4     4
93     Conservative  77                      3                       4     4
   Hague Kennedy Europe political.knowledge gender
8      1       4      1                   0   male
15     1       4     11                   2 female
19     2       4      1                   0   male
35     4       4      3                   2 female
65     3       3      6                   2   male
73     4       4     11                   0 female
80     2       4      3                   2 female
83     4       4     11                   0 female
90     1       5      5                   3   male
93     4       4      8                   3   male
Code
BEPS |> 
  filter(age > 75) |> 
  tail(10)
             vote age economic.cond.national economic.cond.household Blair
1456       Labour  86                      2                       4     2
1463       Labour  76                      5                       3     4
1473       Labour  79                      5                       5     2
1480       Labour  79                      4                       4     5
1487       Labour  79                      3                       3     5
1489       Labour  77                      5                       4     5
1495 Conservative  78                      3                       3     2
1500 Conservative  79                      2                       2     2
1516 Conservative  82                      2                       2     2
1518       Labour  76                      4                       3     2
     Hague Kennedy Europe political.knowledge gender
1456     3       3     11                   1   male
1463     1       3     11                   0 female
1473     1       3      3                   3   male
1480     1       3      3                   3   male
1487     4       4      1                   2   male
1489     4       4      2                   2   male
1495     2       1      6                   2 female
1500     4       4     11                   0 female
1516     1       4     11                   2 female
1518     2       3     11                   2   male
  • Less than: <
Code
BEPS |> 
  filter(age < 35)|> 
  head()
     vote age economic.cond.national economic.cond.household Blair Hague
4  Labour  24                      4                       2     2     1
37 Labour  27                      2                       2     2     2
52 Labour  33                      2                       3     4     4
58 Labour  32                      1                       2     1     4
63 Labour  28                      4                       1     4     2
66 Labour  34                      3                       4     4     4
   Kennedy Europe political.knowledge gender
4        3      4                   0 female
37       3      7                   0 female
52       2      9                   0   male
58       1      1                   2   male
63       2      6                   2 female
66       3      8                   0 female
  • Greater than or equal to: >=
Code
BEPS |> 
  filter(age >= 70)|> 
  head()
           vote age economic.cond.national economic.cond.household Blair Hague
8        Labour  77                      3                       4     4     1
10       Labour  70                      3                       2     5     1
15       Labour  77                      2                       3     2     1
19       Labour  79                      3                       3     4     2
35 Conservative  76                      3                       4     4     4
40 Conservative  72                      1                       3     2     2
   Kennedy Europe political.knowledge gender
8        4      1                   0   male
10       1     11                   2   male
15       4     11                   2 female
19       4      1                   0   male
35       4      3                   2 female
40       2     11                   2 female
  • Less than or equal to: <=
Code
BEPS |> 
  filter(age <= 25)|> 
  head()
                vote age economic.cond.national economic.cond.household Blair
4             Labour  24                      4                       2     2
157           Labour  24                      3                       5     4
222 Liberal Democrat  24                      4                       2     4
224     Conservative  25                      2                       3     1
249 Liberal Democrat  24                      4                       3     4
412           Labour  24                      4                       4     4
    Hague Kennedy Europe political.knowledge gender
4       1       3      4                   0 female
157     1       2      4                   0   male
222     2       4      4                   0 female
224     4       4      7                   2 female
249     1       4      2                   3   male
412     2       2      4                   2 female
  • Equal to: ==
Code
summary(BEPS$age)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  24.00   41.00   53.00   54.18   67.00   93.00 
Code
sum(BEPS$age)/1525
[1] 54.1823
Code
BEPS |> 
  filter(age == 54) |> 
  head()
                vote age economic.cond.national economic.cond.household Blair
47  Liberal Democrat  54                      2                       3     4
189 Liberal Democrat  54                      3                       4     2
197     Conservative  54                      3                       5     2
246           Labour  54                      3                       3     5
248 Liberal Democrat  54                      3                       3     2
288 Liberal Democrat  54                      2                       3     2
    Hague Kennedy Europe political.knowledge gender
47      2       2      7                   2 female
189     2       3     10                   0 female
197     4       2     11                   2 female
246     4       3      1                   0 female
248     2       5      5                   0 female
288     4       4     11                   0 female
Code
# Notice: it is a dobule equal sign
# 
  • Not Equal to: !=

\[ \neq\]

Code
BEPS |> 
  filter(age != 54) |>  # Notice: it is not equal to 54.
  head()
              vote age economic.cond.national economic.cond.household Blair
1 Liberal Democrat  43                      3                       3     4
2           Labour  36                      4                       4     4
3           Labour  35                      4                       4     5
4           Labour  24                      4                       2     2
5           Labour  41                      2                       2     1
6           Labour  47                      3                       4     4
  Hague Kennedy Europe political.knowledge gender
1     1       4      2                   2 female
2     4       4      5                   2   male
3     2       3      3                   2   male
4     1       3      4                   0 female
5     1       4      6                   2   male
6     4       2      4                   2   male

5 Logical operators

  • And: &
Code
BEPS |> 
  filter(age == 54 & gender == "male") |> 
  head()
                vote age economic.cond.national economic.cond.household Blair
363     Conservative  54                      3                       3     4
498 Liberal Democrat  54                      2                       2     1
553     Conservative  54                      4                       3     2
619           Labour  54                      4                       4     2
792           Labour  54                      2                       3     2
794 Liberal Democrat  54                      4                       4     4
    Hague Kennedy Europe political.knowledge gender
363     4       4     11                   1   male
498     2       2     11                   2   male
553     4       3     11                   2   male
619     2       4      3                   2   male
792     1       2      5                   3   male
794     2       4     10                   0   male
  • Or: |
Code
BEPS |> 
  filter(age == 54 | gender == "male") |> 
  head()
              vote age economic.cond.national economic.cond.household Blair
2           Labour  36                      4                       4     4
3           Labour  35                      4                       4     5
5           Labour  41                      2                       2     1
6           Labour  47                      3                       4     4
7 Liberal Democrat  57                      2                       2     4
8           Labour  77                      3                       4     4
  Hague Kennedy Europe political.knowledge gender
2     4       4      5                   2   male
3     2       3      3                   2   male
5     1       4      6                   2   male
6     4       2      4                   2   male
7     4       2     11                   2   male
8     1       4      1                   0   male
  • Not: !
Code
BEPS |> 
  filter(!gender == "male") |> 
  head()
               vote age economic.cond.national economic.cond.household Blair
1  Liberal Democrat  43                      3                       3     4
4            Labour  24                      4                       2     2
9            Labour  39                      3                       3     4
11           Labour  39                      3                       3     1
13           Labour  59                      4                       4     4
14           Labour  66                      3                       3     2
   Hague Kennedy Europe political.knowledge gender
1      1       4      2                   2 female
4      1       3      4                   0 female
9      4       4     11                   0 female
11     2       1      7                   0 female
13     1       4     10                   2 female
14     5       4      8                   0 female
Code
BEPS |> 
  filter(gender != "male") |> 
  head()
               vote age economic.cond.national economic.cond.household Blair
1  Liberal Democrat  43                      3                       3     4
4            Labour  24                      4                       2     2
9            Labour  39                      3                       3     4
11           Labour  39                      3                       3     1
13           Labour  59                      4                       4     4
14           Labour  66                      3                       3     2
   Hague Kennedy Europe political.knowledge gender
1      1       4      2                   2 female
4      1       3      4                   0 female
9      4       4     11                   0 female
11     2       1      7                   0 female
13     1       4     10                   2 female
14     5       4      8                   0 female

6 Order of logical operations

Code
BEPS |> 
  filter(age == 27 & vote == "labor" | gender == "male") |> 
  head()
              vote age economic.cond.national economic.cond.household Blair
2           Labour  36                      4                       4     4
3           Labour  35                      4                       4     5
5           Labour  41                      2                       2     1
6           Labour  47                      3                       4     4
7 Liberal Democrat  57                      2                       2     4
8           Labour  77                      3                       4     4
  Hague Kennedy Europe political.knowledge gender
2     4       4      5                   2   male
3     2       3      3                   2   male
5     1       4      6                   2   male
6     4       2      4                   2   male
7     4       2     11                   2   male
8     1       4      1                   0   male
Code
BEPS |> 
  filter(age == 27 & (vote == "labor" | gender == "male"))
                 vote age economic.cond.national economic.cond.household Blair
475      Conservative  27                      3                       3     2
906  Liberal Democrat  27                      5                       5     2
1167 Liberal Democrat  27                      3                       4     4
1365     Conservative  27                      3                       5     4
     Hague Kennedy Europe political.knowledge gender
475      4       2      8                   2   male
906      1       4      4                   3   male
1167     4       4      2                   3   male
1365     4       4      8                   2   male


Thank you!