Primary exercises

  1. Dietary intakes.
    Four patients had daily dietary intakes of 2314, 2178, 1922, 2004 kcal.
    Make a vector intakesKCal of these four values.
    What is the class of this vector?
    Convert the values into in kJ using 1 kcal = 4.184 kJ.

  2. Combining (appending) vectors.
    Additional set of intakes is provided: 2122, 2616, NA, 1771 kcal.
    Use c() to append the new intakes after values in intakesKCal and store the result in allIntakesKCal.
    Print the combined vector and print its calculated length.

  3. Mean and sum.
    Calculate mean intake for patients in vector intakesKCal.
    Next, calculate mean intake for patients in vector allIntakesKCal.
    Can you explain the result?
    Check help for ?mean, in particular the na.rm argument.
    Use the extra argument na.rm=TRUE to calculate the mean of non-NA elements of allIntakesKCal.
    Check help for ?sum how to omit NA elements in sum calculation.
    Now, calculate the total sum of allIntakesKCal intakes ignoring the NA element.

  4. Selecting and counting (non)missing elements.
    Understand the result of is.na( allIntakesKCal ).
    Now, negate the above result with ! operator.
    Use above vectors as argument to sum to calculate the number of missing and non-missing elements in allIntakesKCal.
    Understand allIntakesKCal[ !is.na( allIntakesKCal ) ].

  5. Descriptive statistics of a vector; normally distributed random numbers.
    The code v <- rnorm( 10 ) would sample 10 numbers from the normal distribution and store them as a vector in v.
    Print v. Then repeat v <- rnorm( 10 ) and print v again. Has v changed? Print v and find by eye the smallest and the largest of these numbers.
    Try to use the functions min and max on v – have you found the same numbers?
    Calculate the mean, median and the standard deviation (sd) of v.

  6. Selecting and counting elements by a condition. Type v < 0 and understand the result.
    How to interpret the number produced by sum( v < 0 )? How to interpret the number produced by sum( !( v < 0 ) )?

  7. Head and tail.
    Often there is a need to quickly look at the beginning (head) or at the end (tail) of a vector.
    Try these functions to show the first 5 and the last 7 elements of a randomly generated vector v <- rnorm( 20 ).

Extra exercises

  1. More complex sequences of numbers.
    Read help (see Help pane) about seq function.
    Use it to generate sequence: 10, 7, 4, 1, -2, -5.
    Understand the error message of seq( 10, -5, 3 ).

  2. Repetitions.
    Read help (see Help pane) about rep function.
    Use it to generate sequence: 0, 1, 0, 1, 0, 1.

  3. Type conversion to a character vector.
    Sometimes it is necessary to convert a numerical vector to a character vector.
    Understand what the function as.character does for argument 1:5.

  4. Type conversion to a numerical vector.
    Sometimes it is necessary to convert a character vector to a numerical vector.
    Understand what the function as.numeric does for argument c( "1", "-1", "x" ).
    Note the warning message. Why is there NA?

  5. Naming vector elements.
    It is possible to give names to vector elements.
    Define ages <- c( Amy = 10, 'Dan' = 6, "Eve" = 11, "Eve 2" = 3, Grandma = NA ).
    Print ages and understand names( ages ).
    Use square brackets to access age of Dan. Try also for Eve 2.

  6. (ADV) Write a text vector to a file and read it back.
    This exercise demonstrates writing a single-column vector (later multicolumn tables will be discussed).
    First choose a name for the file (e.g. test.txt) and store it in the variable fileName.
    Next, create a character/text vector v with several text elements.
    Check manual for writeLines and try writeLines(v) to see in the console what will be written to a file.
    Now, set the argument con = fileName and write to the file.
    Use readLines( con = fileName ) to read the file and put it back to variable w.
    Understand identical( v, w ).

  7. (ADV) Write/read a numerical vector; problems.
    In the previous exercise change v to be a vector of some numbers.
    Use as.character to make writeLines work (do not change v).
    Why identical( v, w ) fails? Check class(v) and class(w).
    What conversions of w would be needed to make identical work?



Copyright © 2023 Biomedical Data Sciences (BDS) | LUMC