Primary exercises

On the survey dataset:

  1. Report mean hand spans {span1,span2} per gender.
survey %>% group_by(gender) %>% 
           summarise(meanWritingHand=mean(span1),meanNonWritingHand=mean(span2), .groups='drop')
# A tibble: 2 × 3
  gender meanWritingHand meanNonWritingHand
  <chr>            <dbl>              <dbl>
1 female            17.6               17.5
2 male              19.7               19.7
  1. Report mean and median age per gender, including the size of each gender group.
survey %>% group_by(gender) %>% 
           summarise(size=n(), meanAge=mean(age), medianAge=median(age), .groups='drop')
# A tibble: 2 × 4
  gender  size meanAge medianAge
  <chr>  <int>   <dbl>     <dbl>
1 female   117    20.4      18.4
2 male     116    20.3      18.9
  1. Report mean pulse per gender and exercise group, including size of each group. Do the same for gender and smokes group.
# gender,exercise group
survey %>% group_by(gender, exercise) %>% 
           summarise(size=n(), meanPulse=mean(pulse, na.rm=TRUE), .groups='drop')
# A tibble: 6 × 4
  gender exercise  size meanPulse
  <chr>  <chr>    <int>     <dbl>
1 female freq        48      73.7
2 female none        11      71.4
3 female some        58      77  
4 male   freq        65      70.7
5 male   none        12      80.6
6 male   some        39      75.5
# gender,smokes group
survey %>% group_by(gender, smokes) %>% 
           summarise(size=n(), meanPulse=mean(pulse, na.rm=TRUE), .groups='drop')
# A tibble: 8 × 4
  gender smokes  size meanPulse
  <chr>  <chr>  <int>     <dbl>
1 female heavy      5      75  
2 female never     98      75.7
3 female occas      9      73.4
4 female regul      5      69.2
5 male   heavy      6      82.7
6 male   never     88      72.4
7 male   occas     10      74.5
8 male   regul     12      75.2
  1. Report the mean age of those who exercise frequently per gender including the group size
# Solution 1:
survey %>% filter(exercise=='freq') %>% 
           group_by(gender) %>% 
           summarise(size=n(), meanAgeFreqExercise=mean(age), .groups='drop')
# A tibble: 2 × 3
  gender  size meanAgeFreqExercise
  <chr>  <int>               <dbl>
1 female    48                20.2
2 male      65                20.5
# Solution 2: first group and summarise and then filter out exercise group 'freq'. This is a 
# less optimal solution since it calculates the summary on all exercise groups, whereas the 
# solution 1 calculates the summary on a smaller table with only 'freq' exercise group.
# 
# survey %>% group_by(gender,exercise) %>%
#            summarise(size=n(), meanAgeFreqExercise=mean(age), .groups='drop') %>%
#            filter(exercise=='freq') %>%
#            select(-exercise)
  1. Recall primary exercise (7) section Summarise, do the same using group_by.
# Note: that additional argument '.groups = "drop"' is added to suppress tidyverse message.
 survey %>%  
  group_by(gender,smokes) %>% 
  summarise(meidan_age=median(age), .groups = "drop")  %>% 
  filter(smokes=='heavy')
# A tibble: 2 × 3
  gender smokes meidan_age
  <chr>  <chr>       <dbl>
1 female heavy        18.4
2 male   heavy        20.5

Extra exercises

  1. Produce the following results:

1.a frequency of hand categories per gender.

survey %>%  group_by(gender,hand) %>%  summarise(n=n())
# A tibble: 4 × 3
# Groups:   gender [2]
  gender hand      n
  <chr>  <chr> <int>
1 female left      7
2 female right   110
3 male   left     10
4 male   right   106

1.b frequency of fold categories of females.

survey %>%  filter(gender=="female") %>% group_by(gender,fold) %>% summarise(n=n(), .groups = "drop")
# A tibble: 3 × 3
  gender fold        n
  <chr>  <chr>   <int>
1 female left       47
2 female neither     6
3 female right      64
# Alternative solution to b) however less efficient, since the grouping is now done on a 
# larger set including male observations. 
# 
# survey %>%   group_by(gender,fold)  %>% summarise(n=n(), .groups = "drop") %>% filter(gender=="female")

1.c most popular name initials in descending order of popularity. See ?stringr::str_sub for extracting initials from a character vector.

survey %>% mutate(initial=str_sub(name, 1,1)) %>% group_by(initial) %>% summarise(n=n()) %>% arrange(desc(n))
# A tibble: 20 × 2
   initial     n
   <chr>   <int>
 1 C          25
 2 J          21
 3 L          19
 4 M          18
 5 A          17
 6 G          17
 7 B          15
 8 R          14
 9 D          12
10 E          11
11 T          10
12 V           9
13 F           8
14 H           8
15 S           8
16 W           6
17 N           5
18 K           4
19 P           4
20 I           2


Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC