Send your solutions by email to the lecturer (firstname.lastname@example.org), at the latest on Mon 7 Feb at 10.00 using R1 as the title of your message. Also include your name and student number in the message.
In most cases, your solutions should be a few lines of R code. Please, send the solutions as plain text (do not format your code with a word processing program).
Create a directory (folder) where you put files that you need during the course (instructions). You do not need to document your solution to this exercise, even though this is the most important R exercise in the course. You will get automatically credit for this exercise, if you send solutions to any of the other R exercises.
R has a function called
What are the names of its formal arguments?
Which values are bound to the formal arguments in the following call?
z <- 3 pnorm(1, lower = (1 < 2), 2, mean = z)
First create vector
contains 100 random values drawn from the Poisson distribution.
x <- rpois(100, 1.1)
Formulate your answers to the following questions so that they work not only for your particular sample but for any random sample drawn as above.
xat the positions 2, 3 and 20?
bwhose i'th entry is TRUE if and only if the i'th entry of
xhave the value zero?
Now we want to find the indices of those entries of vector
(generated in the previous exercise) which are greater than or equal to 2.
One way of doing this is the following.
which(x >= 2)
Now you should try to achieve the same result without using
Instead, you should index with a suitable logical vector the
inds which you generate as follows.
inds <- 1:length(x)
The following lines create vector
x which contains a random number of
missing values (NA's).
n <- 100 x <- rnorm(n) x[rbinom(n, 1, 0.1) == 1] <- NA
Suppose that you want to plot 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).
The result is a line plot, where the point
is connected to the points
(x, y) and so on.
Since the x-values are not ordered, the plot looks like a spider's web.
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
prior to plotting. How do you do this in practice? Pretend that you do
not know the rule of calculating the y's from the x's.
Last updated 2011-02-01 14:35
petri.koistinen 'at' helsinki.fi