Imperial College/Courses/Fall2008/Synthetic Biology (MRes class)/'R' Tutorial/Practical: Difference between revisions
No edit summary |
No edit summary |
||
Line 16: | Line 16: | ||
</div> | </div> | ||
<br style="clear:both"> | <br style="clear:both"> | ||
==Exercise 1== | |||
* First create a vector x, which contains 100 random values drawn from the standard normal distribution. | |||
** x <- rnorm(100) | |||
# How do you form a vector which contains the entries of x at the positions 2, 30 and 67? | |||
# How do you form a vector which contains all the entries of x except the first and the second? | |||
# 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. | |||
# How do you select those entries of x which are greater than -1.5 and less than 1? | |||
# 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== | |||
# Write an expression which produces a vector with the entries 0, 10, ..., 50, 60 followed by 11 equally spaced values from 70 to 100. | |||
# 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? |
Revision as of 15:44, 3 December 2008
<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)
- How do you form a vector which contains the entries of x at the positions 2, 30 and 67?
- How do you form a vector which contains all the entries of x except the first and the second?
- 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.
- How do you select those entries of x which are greater than -1.5 and less than 1?
- 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
- Write an expression which produces a vector with the entries 0, 10, ..., 50, 60 followed by 11 equally spaced values from 70 to 100.
- 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?