Primary exercises

In the survey data:

  1. Take a look at the table with glimpse and inspect its dimensions and types.
survey %>% glimpse()
Rows: 233
Columns: 13
$ name     <chr> "Alyson", "Todd", "Gerald", "Robert", "Dustin", "Abby", "Andre", "Michael", "Edward", "Carl", "Noemi", "Alfred", "Bernice",…
$ gender   <chr> "female", "male", "male", "male", "male", "female", "male", "female", "male", "male", "female", "male", "female", "female",…
$ span1    <dbl> 18.5, 19.5, 18.0, 18.8, 20.0, 18.0, 17.7, 17.0, 20.0, 18.5, 17.0, 21.0, 16.0, 19.5, 16.0, 17.5, 18.0, 19.4, 20.5, 21.0, 21.…
$ span2    <dbl> 18.0, 20.5, 13.3, 18.9, 20.0, 17.7, 17.7, 17.3, 19.5, 18.5, 17.2, 21.0, 16.0, 20.2, 15.5, 17.0, 18.0, 19.2, 20.5, 20.9, 22.…
$ hand     <chr> "right", "left", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right"…
$ fold     <chr> "right", "right", "left", "right", "neither", "left", "left", "right", "right", "right", "left", "right", "left", "left", "…
$ pulse    <dbl> 92, 104, 87, NA, 35, 64, 83, 74, 72, 90, 80, 68, NA, 66, 60, NA, 89, 74, NA, 78, 72, 72, 64, 62, 64, 90, 90, 62, 76, 79, 76…
$ clap     <chr> "left", "left", "neither", "neither", "right", "right", "right", "right", "right", "right", "right", "left", "right", "neit…
$ exercise <chr> "some", "none", "none", "none", "some", "some", "freq", "freq", "some", "some", "freq", "freq", "some", "some", "some", "fr…
$ smokes   <chr> "never", "regul", "occas", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never…
$ height   <dbl> 173.00, 177.80, NA, 160.00, 165.00, 172.72, 182.88, 157.00, 175.00, 167.00, 156.20, NA, 155.00, 155.00, NA, 156.00, 157.00,…
$ m.i      <chr> "metric", "imperial", NA, "metric", "metric", "imperial", "imperial", "metric", "metric", "metric", "imperial", NA, "metric…
$ age      <dbl> 18.250, 17.583, 16.917, 20.333, 23.667, 21.000, 18.833, 35.833, 19.000, 22.333, 28.500, 18.250, 18.750, 17.500, 17.167, 17.…
  1. Summarise on mean hand spans {span1,span2} and median pulse.
# 'pulse' variable has missing values (NA), therefore the use 
# of 'na.rm=TRUE' in the function 'median'.
#
survey %>% summarise(meanWritingHand=mean(span1), 
                     meanNonWritingHand=mean(span2), 
                     medianPulse=median(pulse, na.rm=TRUE))
# A tibble: 1 × 3
  meanWritingHand meanNonWritingHand medianPulse
            <dbl>              <dbl>       <dbl>
1            18.7               18.6          73
  1. Summarise on mean age and median height.
survey %>% summarise(meanAge=mean(age), medianHeight=median(height, na.rm=TRUE))
# A tibble: 1 × 2
  meanAge medianHeight
    <dbl>        <dbl>
1    20.4          171
  1. Count the number of males and females.
survey %>% count(gender)
# A tibble: 2 × 2
  gender     n
  <chr>  <int>
1 female   117
2 male     116
  1. Produce the frequency tables on {gender, fold} and {gender, fold, clap}.
survey %>% count(gender, fold)
# A tibble: 6 × 3
  gender fold        n
  <chr>  <chr>   <int>
1 female left       47
2 female neither     6
3 female right      64
4 male   left       49
5 male   neither    12
6 male   right      55
survey %>% count(gender, fold, clap)
# A tibble: 15 × 4
   gender fold    clap        n
   <chr>  <chr>   <chr>   <int>
 1 female left    left        8
 2 female left    neither     8
 3 female left    right      31
 4 female neither right       6
 5 female right   left       12
 6 female right   neither    16
 7 female right   right      36
 8 male   left    left        7
 9 male   left    neither    13
10 male   left    right      29
11 male   neither neither     2
12 male   neither right      10
13 male   right   left       10
14 male   right   neither    10
15 male   right   right      35
  1. Produce the frequency table on gender and smokes, show only females.
survey %>% count(gender,smokes) %>% filter(gender=="female") 
# A tibble: 4 × 3
  gender smokes     n
  <chr>  <chr>  <int>
1 female heavy      5
2 female never     98
3 female occas      9
4 female regul      5
  1. Summarise median age of male heavy smokers. Do the same for females.
# male
survey %>%  filter(gender=='male' & smokes=='heavy') %>%  summarise(median_age=median(age))
# A tibble: 1 × 1
  median_age
       <dbl>
1       20.5
# female
survey %>%  filter(gender=='female' & smokes=='heavy') %>%  summarise(median_age=median(age))
# A tibble: 1 × 1
  median_age
       <dbl>
1       18.4

Extra exercises

  1. Produce the frequency table on gender, exercise and smokes but only for male smokers.
# smoker is someone that smokes 'occas', 'regul' or 'heavy', i.e. smokes !='never'
#
survey %>% filter(gender=="male" & smokes!="never") %>% count(gender,exercise, smokes)
# A tibble: 9 × 4
  gender exercise smokes     n
  <chr>  <chr>    <chr>  <int>
1 male   freq     heavy      4
2 male   freq     occas      7
3 male   freq     regul      7
4 male   none     heavy      1
5 male   none     occas      2
6 male   none     regul      1
7 male   some     heavy      1
8 male   some     occas      1
9 male   some     regul      4
  1. Do individuals all have different names? Put differently, is each observation uniquely identifiable with the variable name?
# The answer is no, because the number of unique names is less than 
# total number of observations:
#
nrow( survey %>% distinct(name)) == nrow(survey)
[1] FALSE
  1. Find pairs of variables which can uniquely identify each observation. Use name and one other variable for this purpose.
# We see here that name and age combination is unique in the entire table.
nrow( survey %>% distinct(name, age)) == nrow(survey)
[1] TRUE
nrow( survey %>% distinct(name, height)) == nrow(survey) # The same is true for name and height
[1] TRUE
  1. An alternative solution for the primary exercise (6) is solution (2) below:
# solution (1)
survey %>% count(gender,smokes) %>% filter(gender=="female") 
# A tibble: 4 × 3
  gender smokes     n
  <chr>  <chr>  <int>
1 female heavy      5
2 female never     98
3 female occas      9
4 female regul      5
# solution (2)
survey %>% filter(gender=="female") %>% count(gender,smokes)
# A tibble: 4 × 3
  gender smokes     n
  <chr>  <chr>  <int>
1 female heavy      5
2 female never     98
3 female occas      9
4 female regul      5

where first filter is applied and then count. You may check the results and see that the outcomes are identical. Explain the difference between the two solutions.

# The alternative, solution (2), is more efficient, because 'count(gender, smokes)' has 
# less rows (only 'females') to work with, whereas  solution (1) counts both males and 
# females upon which the males are filtered out with filter(...).


Copyright © 2023 Biomedical Data Sciences (BDS) | LUMC