Imperial College/Courses/Fall2008/Synthetic Biology (MRes class)/'R' Tutorial/Practical: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
No edit summary
Line 46: Line 46:


The result was a line plot, where the point (x[1], y[1]) is connected to the points (x[2], y[2]), (x[3], y[3]) and so on. Since the x-values are not ordered, the line plot looks messy. Instead, you want a line plot which resembles the graph of the sine function. The trick is to sort the x vector into increasing order, and to apply the same permutation also to the y vector prior to plotting. How do you do this in practice?
The result was a line plot, where the point (x[1], y[1]) is connected to the points (x[2], y[2]), (x[3], y[3]) and so on. Since the x-values are not ordered, the line plot looks messy. Instead, you want a line plot which resembles the graph of the sine function. The trick is to sort the x vector into increasing order, and to apply the same permutation also to the y vector prior to plotting. How do you do this in practice?
==Exercise 4==
In the U.S. temperatures are usually expressed in degrees Fahrenheit (F) instead of degrees Celsius (C), which are used in the rest of the world. The conversion formula between the two temperature scales is the following.
C = 5 / 9 * (F - 32)
Write function FtoC which converts temperatures given in degrees Fahrenheit into degrees Celsius. Also write function CtoF which converts temperatures given in degrees Celsius into degrees Fahrenheit.
==Exercise 5==
Supposedly, C. F. Gauss was given at school at the age of seven the problem of summing the integers 1, 2, ..., 100, and found the correct answer almost instantly (without having been told about the arithmetic series).
# How do you find that sum by using the function sum()?
# For the sake of practicing, do the same calculation using a for-loop. Of course, your first solution, with the sum-function, is much clearer and shorter.

Revision as of 16:04, 3 December 2008

Fall 2008 - Synthetic Biology (MRes class)

Home        'R' Tutorial        Resources        Literature

<html> <body> <!-- Start of StatCounter Code --> <script type="text/javascript"> var sc_project=3315864; var sc_invisible=0; var sc_partition=36; var sc_security="8bb2efcd"; </script>

<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/"><img class="statcounter" src="http://c37.statcounter.com/3315864/0/8bb2efcd/0/" alt="blog stats" /></a></div></noscript> <!-- End of StatCounter Code -->

</body> </html>

Introduction to 'R'



Exercise 1

  • First create a vector x, which contains 100 random values drawn from the standard normal distribution.
    • x <- rnorm(100)
  1. How do you form a vector which contains the entries of x at the positions 2, 30 and 67?
  2. How do you form a vector which contains all the entries of x except the first and the second?
  3. How do you create a logical vector b, whose i'th entry is TRUE if and only if the i'th entry of x is greater than -1.5.
  4. How do you select those entries of x which are greater than -1.5 and less than 1?
  5. How do you find out, how many entries of x are greater than -1.5 and less than 1?

(Try to formulate your answers so that they work not only for your particular random sample but for any random sample drawn as above.)

Exercise 2

  1. Write an expression which produces a vector with the entries 0, 10, ..., 50, 60 followed by 11 equally spaced values from 70 to 100.
  2. By which command do you find out the length of the generated vector?

Exercise 3

Suppose that you have to make a line plot of data which resembles the data we generate as follows.

  • x <- runif(100, -pi, pi)
  • y <- sin(x)

Here we first sample 100 value uniformly on the interval (-pi, pi) and then calculate the sine function.

Try the command:

  • plot(x, y, type = 'l')

(there is a lower case L inside the quotation marks) and notice that the resulting line drawing does not resemble the graph of the sine function.

The result was a line plot, where the point (x[1], y[1]) is connected to the points (x[2], y[2]), (x[3], y[3]) and so on. Since the x-values are not ordered, the line plot looks messy. Instead, you want a line plot which resembles the graph of the sine function. The trick is to sort the x vector into increasing order, and to apply the same permutation also to the y vector prior to plotting. How do you do this in practice?

Exercise 4

In the U.S. temperatures are usually expressed in degrees Fahrenheit (F) instead of degrees Celsius (C), which are used in the rest of the world. The conversion formula between the two temperature scales is the following.

C = 5 / 9 * (F - 32)

Write function FtoC which converts temperatures given in degrees Fahrenheit into degrees Celsius. Also write function CtoF which converts temperatures given in degrees Celsius into degrees Fahrenheit.

Exercise 5

Supposedly, C. F. Gauss was given at school at the age of seven the problem of summing the integers 1, 2, ..., 100, and found the correct answer almost instantly (without having been told about the arithmetic series).

  1. How do you find that sum by using the function sum()?
  2. For the sake of practicing, do the same calculation using a for-loop. Of course, your first solution, with the sum-function, is much clearer and shorter.