R tasks deadline: Wednesday, 18 December
Please, create a function for each task. Functions should be named as task names,
usually, as task1, task2, etc. Here are some examples of functions:
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 until you get all the toys. Then compute an average number of steps you needed to finish.
R datasets. deadline: Wednesday, 18 December
- Create a script (not a function)
task1.Rwith the following computations:- Save dataframe
Cars93from the libraryMASSto the variablecars. - Add the column
kpl(kilometers per liter) to the dataframe, use a columnMPG.city(miles per gallon) - Select all cars with
Horsepowergreater than 200. - Plot Horsepower vs kpl. Make informative labels for axes.
- add a linear regression line.
- Make a copy of the
carsdataframe, that has only columnsHorsepower,kpl, andwt(weight in tonns, use the originalWeightcolumn that has weights in pounds). Pass this copy as the only argument for theplotfunction. What do you see on this plot?
- Save dataframe
- Create a script (not a function)
task2.R.- Create a new dataframe about balls, you will use random values for it. It should have two columns
sizeandcolor. The first one is a factor with levels “big” and “small”, the second one is a factor with three levels “red”, “green”, “blue”. Usesampleto create a create a vector of 1000 random values. - Apply the following functions to this dataframe:
table,summary,plot. How many big green balls are there? - Add the column
weightthis should be normally distributed random numbers with different means for different balls sizes. - Call the functions
table,summary,plotagain. What do they show? - Try to make a plot of weight vs. size, but points on the plot should have the same color as the corresponding ball.
- Create a new dataframe about balls, you will use random values for it. It should have two columns