R programming for beginners (GV900) ~ Solutions

Lesson 3: Practice exercises

Homework
Author
Published

Thursday, December 28, 2023

Modified

Sunday, January 7, 2024


Setup

Code
library(tidyverse)

library(carData)

Task 1: filter rows in dataset BEPS

  1. age <= 27
Code
BEPS |> 
  filter(age <= 27) |> 
  head()
                vote age economic.cond.national economic.cond.household Blair
4             Labour  24                      4                       2     2
37            Labour  27                      2                       2     2
157           Labour  24                      3                       5     4
173 Liberal Democrat  26                      4                       4     4
222 Liberal Democrat  24                      4                       2     4
224     Conservative  25                      2                       3     1
    Hague Kennedy Europe political.knowledge gender
4       1       3      4                   0 female
37      2       3      7                   0 female
157     1       2      4                   0   male
173     2       4      5                   2 female
222     2       4      4                   0 female
224     4       4      7                   2 female
  1. 35 <= age <= 55
Code
BEPS |> 
  filter(age >= 35 & age <= 55) |> 
  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
5           Labour  41                      2                       2     1
6           Labour  47                      3                       4     4
9           Labour  39                      3                       3     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
5     1       4      6                   2   male
6     4       2      4                   2   male
9     4       4     11                   0 female
  1. age == 54 and vote is Labour and gender is female
Code
BEPS |> 
  filter(age == 54 & vote == "Labour" & gender == "female")
       vote age economic.cond.national economic.cond.household Blair Hague
246  Labour  54                      3                       3     5     4
556  Labour  54                      5                       3     5     2
1098 Labour  54                      3                       3     4     2
1273 Labour  54                      3                       3     4     2
1415 Labour  54                      3                       4     4     2
1419 Labour  54                      4                       3     2     4
1474 Labour  54                      3                       3     4     1
     Kennedy Europe political.knowledge gender
246        3      1                   0 female
556        3      6                   2 female
1098       2      1                   2 female
1273       2      3                   2 female
1415       2      3                   2 female
1419       4      6                   0 female
1474       2      5                   0 female
  1. meet either condition: age = 54; vote is Conservative or gender is male
Code
BEPS |> 
  filter(age == 54 | (vote == "Conservative" | 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
  1. political.knowledge is not zero.
Code
BEPS |> 
  filter(political.knowledge != 0) |> 
  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
5           Labour  41                      2                       2     1
6           Labour  47                      3                       4     4
7 Liberal Democrat  57                      2                       2     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
5     1       4      6                   2   male
6     4       2      4                   2   male
7     4       2     11                   2   male
  1. Sort Europe from high to low
Code
BEPS |> 
  arrange(Europe) |> 
  head()
     vote age economic.cond.national economic.cond.household Blair Hague
8  Labour  77                      3                       4     4     1
19 Labour  79                      3                       3     4     2
23 Labour  59                      3                       3     4     2
29 Labour  44                      3                       3     4     2
30 Labour  61                      4                       3     5     1
33 Labour  59                      3                       3     2     2
   Kennedy Europe political.knowledge gender
8        4      1                   0   male
19       4      1                   0   male
23       2      1                   2   male
29       4      1                   2   male
30       2      1                   2   male
33       2      1                   2 female

Task 2: select columns

  1. select vote, age and gender and rename them as Party, Age, and Sex, then filter those age >= 35 and gender is female
Code
BEPS |>
  select(Party = vote,
         Age = age,
         Sex = gender) |>
  filter(Age >= 35 & Sex == "female") |>
  head()
              Party Age    Sex
1  Liberal Democrat  43 female
9            Labour  39 female
11           Labour  39 female
13           Labour  59 female
14           Labour  66 female
15           Labour  77 female
  1. select vote, Europe and political.knowledge, then sort political.knowledge from high to low and vote by alphabetic order.
Code
BEPS |>
  select(vote, Europe, political.knowledge) |>
  arrange(-political.knowledge, vote) |>
  head()
            vote Europe political.knowledge
46  Conservative      6                   3
92  Conservative      8                   3
93  Conservative      8                   3
139 Conservative     10                   3
179 Conservative      8                   3
182 Conservative     11                   3
  1. select all variables but Blair, Hague, Kennedy
Code
BEPS |> 
  select(-Blair, -Hague, -Kennedy) |> 
  head()
              vote age economic.cond.national economic.cond.household Europe
1 Liberal Democrat  43                      3                       3      2
2           Labour  36                      4                       4      5
3           Labour  35                      4                       4      3
4           Labour  24                      4                       2      4
5           Labour  41                      2                       2      6
6           Labour  47                      3                       4      4
  political.knowledge gender
1                   2 female
2                   2   male
3                   2   male
4                   0 female
5                   2   male
6                   2   male

Task 3: mutate new variables

  1. Create a new variable which is the sum of Blair, Hague, Kennedy, name it “BHK”, and put it in front of Blair.
Code
BEPS |> 
  mutate(BHK = Blair + Hague + Kennedy,
         .before = Blair) |> 
  head()
              vote age economic.cond.national economic.cond.household BHK Blair
1 Liberal Democrat  43                      3                       3   9     4
2           Labour  36                      4                       4  12     4
3           Labour  35                      4                       4  10     5
4           Labour  24                      4                       2   6     2
5           Labour  41                      2                       2   6     1
6           Labour  47                      3                       4  10     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
  1. Create a new variable named “retired”, with 1 over age 65, 0 others, then put it after variable age.
Code
BEPS |> 
  mutate(regired = ifelse(age > 65, 1, 0),
         .after = age) |> 
  head()
              vote age regired economic.cond.national economic.cond.household
1 Liberal Democrat  43       0                      3                       3
2           Labour  36       0                      4                       4
3           Labour  35       0                      4                       4
4           Labour  24       0                      4                       2
5           Labour  41       0                      2                       2
6           Labour  47       0                      3                       4
  Blair Hague Kennedy Europe political.knowledge gender
1     4     1       4      2                   2 female
2     4     4       4      5                   2   male
3     5     2       3      3                   2   male
4     2     1       3      4                   0 female
5     1     1       4      6                   2   male
6     4     4       2      4                   2   male

Task 4: group and summarise

  1. group gender, then count the number of male and female, and calculate average age and average political.knowledge
Code
BEPS |> 
  group_by(gender) |> 
  summarise(counts = n(),
            ave_age = mean(age),
            ave_pol_know = mean(political.knowledge)) 
# A tibble: 2 × 4
  gender counts ave_age ave_pol_know
  <fct>   <int>   <dbl>        <dbl>
1 female    812    54.5         1.38
2 male      713    53.9         1.73
  1. group vote and gender, then count the number, and find out the maximum age and minimum age.
Code
BEPS |> 
  group_by(vote, gender) |> 
  summarise(counts = n(),
            max_age = max(age),
            min_age = min(age))
# A tibble: 6 × 5
# Groups:   vote [3]
  vote             gender counts max_age min_age
  <fct>            <fct>   <int>   <int>   <int>
1 Conservative     female    259      93      24
2 Conservative     male      203      89      24
3 Labour           female    372      89      24
4 Labour           male      348      91      24
5 Liberal Democrat female    181      86      24
6 Liberal Democrat male      162      89      24
Back to top