Primary exercises

  1. For weight and height measurements we have the metric (cm,kg) and imperial (inch,lbs) systems. The relation between the measurements in different systems is (1 kg = 2.2 lbs) and (1 inch = 2.54 cm). Write the following unit conversion functions:
    • cm2meter : convert cm to meter
    • lbs2kg : convert weight unit pound to kg
    • inch2cm : convert inch to cm
# Some example calls to the functions:
> inch2cm(70)
[1] 177.8
> lbs2kg(10)
[1] 4.545455
> cm2m(190)
[1] 1.9
  1. Write a function for the survey dataset to report a summary on average pulse and the number of individuals in the given age range. Call it ageRangePulseSummary and it should take 3 arguments, the survey data, minimum age and maximum age, for example:
ageRangePulseSummary(survey, 16,17)
# A tibble: 1 × 2
  meanpulse individuals
      <dbl>       <int>
1      76.2           4
  1. Take the function weightRangeSummary in the lecture notes, make the necessary update such that it includes the weight intervals.

Extra exercises

  1. Write a function odd which takes a number as argument and returns a logical TRUE if the number is an odd number and FALSE otherwise. A whole number is odd when it is not integer divisible by 2. We can check whether a number is divisible by 2 by using the integer division remainder operator %%, known as modulo. The operator returns the remainder of integer (whole number) division:
9 %% 2  # remainder after dividing 9 by 2 
[1] 1
8 %% 2  # remainder after dividing 8 by 2
[1] 0
1:10 %% 2 # remainder after dividing 1..10 by 2
 [1] 1 0 1 0 1 0 1 0 1 0

As you can see the outcome is 0 if the number at the left is integer divisible by 2 and 1 if not. We can use this fact to test whether a number is odd, i.e. not divisible by 2.

  1. Write the function even which tests whether a number is even.

  2. Write a function divisible which given values x and m tests whether x is integer divisible by m.



Copyright © 2023 Biomedical Data Sciences (BDS) | LUMC