Primary exercises

In the survey dataset:

  1. The variables span1 and span2 are of the same type representing the span of the writing hand and the non-writing hand respectively. We would like to analyse these values in groups. To do this we need to convert the variables span1 and span2 into variables span and size with span ranging over span1 and span2 and size over the values of span1 and span2 variables.
survey %>% pivot_longer(c(span1,span2), names_to = "span", values_to = "size")
# A tibble: 466 × 13
   name   gender hand  fold    pulse clap    exercise smokes height m.i        age span   size
   <chr>  <chr>  <chr> <chr>   <dbl> <chr>   <chr>    <chr>   <dbl> <chr>    <dbl> <chr> <dbl>
 1 Alyson female right right      92 left    some     never    173  metric    18.2 span1  18.5
 2 Alyson female right right      92 left    some     never    173  metric    18.2 span2  18  
 3 Todd   male   left  right     104 left    none     regul    178. imperial  17.6 span1  19.5
 4 Todd   male   left  right     104 left    none     regul    178. imperial  17.6 span2  20.5
 5 Gerald male   right left       87 neither none     occas     NA  <NA>      16.9 span1  18  
 6 Gerald male   right left       87 neither none     occas     NA  <NA>      16.9 span2  13.3
 7 Robert male   right right      NA neither none     never    160  metric    20.3 span1  18.8
 8 Robert male   right right      NA neither none     never    160  metric    20.3 span2  18.9
 9 Dustin male   right neither    35 right   some     never    165  metric    23.7 span1  20  
10 Dustin male   right neither    35 right   some     never    165  metric    23.7 span2  20  
# … with 456 more rows
  1. Reproduce the following plot:
ggplot(survey %>% pivot_longer(c(span1,span2), names_to = "span", values_to = "size")) + 
  aes(x=gender, y=size, color=span) + geom_boxplot()

Extra exercises

  1. Produce the following count summary per gender from variables fold and clap.
survey %>% 
  select(gender,fold,clap) %>% 
  pivot_longer(!gender, names_to = "action", values_to = "side") %>% 
  group_by(gender,side) %>% 
  summarise(count=n(), .groups='drop') %>% 
  pivot_wider(names_from = "side",values_from = "count")
# A tibble: 2 × 4
  gender  left neither right
  <chr>  <int>   <int> <int>
1 female    67      30   137
2 male      66      37   129
  1. Often reshaping of the variables are necessary to visualize the information with ggplot. From pulse dataset reproduce the boxplot shown below. Here level is the values of pulse1 and pulse2.
ggplot(pulse %>% 
        drop_na() %>% 
        mutate(exercise=fct_relevel(exercise, c("low","moderate","high"))) %>%
        pivot_longer(c(pulse1,pulse2), names_to = "pulse",values_to = "level") ) + 
  aes(x=exercise, y=level, color=pulse) + geom_boxplot()



Copyright © 2023 Biomedical Data Sciences (BDS) | LUMC