R basics.
Please, create functions for each of the tasks. Functions shouls be named as task names, usually, as task1, task2, etc.
Here are some function examples:
fun1 <- function(x, y) { # here are arguments in the brackets
x + y # the result of the function is its last expression
}
# let's now call the function
fun1(10, 20) # this is 30
# another example
fun2 <- function(n) {
c(1:n, n:1)
}
# let's test the function
fun2(5) # this is a vector 1, 2, 3, 4, 5, 5, 4, 3, 2, 1
fun3 <- function() {
x <- rbinom(20, size=1, prob=0.5) # toss a coin 20 times
mean(x) # this is the result of a function, because this is the last expression
}
fun3() # the result is always different, but it should be about 0.5
- task1 Read help about the
repfunction (typehelp(rep)). Then use it to produce a vector of repeating numbers 1, 2, and 3: \(1, 2, 3, 1, 2, 3, 1, 2, 3, \ldots\). The length of the vector should be 40.- task1a You are given integers
nandsize, create a vector of repeating numbers from1tonof sizesize, for example,task1a(3, 10)should returnc(1, 2, 3, 1, 2, 3, 1, 2, 3, 1).
- task1a You are given integers
- task2 You are given integers
nandsize. Using the functionsample(read help if needed) generate a vector of sizesizeconsisting of random integers from1ton, and return it. For example,task2(3, 5)may return1, 3, 2, 3, 3.- task2a call task2a with the arguments n=5 and size=100, then use functions
tableto find out, how many times each number was generated, use print to print the table from inside the function.
- task2a call task2a with the arguments n=5 and size=100, then use functions
- filter.k You are given a vector
xand a numberk. Return a new vector, that is a copy ofxbut without elements equal tok. For example,filter.k(c(1, 2, 3, 4, 3, 2, 1), 3)should return1, 2, 4, 2, 1. - random.walk.1d You are given an integer
steps, generate a random vector of sizestepsconsisting of numbers 1 and -1. Return the sum of its elements. - mixed.distribution. You are given an integer
size. Generate a vector of sizesizeusing the following algorithm: to generate the next number, toss a coin (50%/50% of head and tails). If you get heads, generate a number from distribution\(N(0, 1)\). Otherwise, from a uniform distribution withmin=-1andmax=1. - random.walk.2d Random walk on a plane. You are given an integer
steps. Generatestepstimes a pair of numbers, this pair may be either (1 0), (-1 0), (0 1), or (0 -1). Put all this pairs in one vector, so you get a vector of size2 * size. This pairs correspond to movements of a point on a plane, a pair is a change for x and y coordinates correspondingly. That is a point moves either up, left, right or down. Finally, sum all x coordinates (all odd indexes), then sum all y coordinates (all even indexes), and you will get two coordinates of where a point had come after its random walk.- plot.random.walk You are given interers
nandsize. Call the previous function with thesizeargumentntimes. Plot all thenpoints.
- plot.random.walk You are given interers
- kinder.surprise There are
npossible toys inside a kinder surprise. How many kinder surprises should one buy in avarage to get all the toys? Make 10000 experiments, where you repeat opening kinder surprises untill you get all the toys. Then compute an avarage number of steps you needed to finish.