## assignment for lecture 11. To hand in before Friday December 9th, 18.00.
### This time, let's start with some rehersal
## (1)
# Make a for loop with 50 runs, in each run of which which you:
# - sample and assign to a variable 100 uniform numbers between -5 and 5 (runif())
# - sample and assign to a variable 100 normal numbers (mean = 0, sd = 5) (rnorm())
# - do a single sample t-test ("is the mean equal to zero?") to each of the two samples.
# - attach the p-value of each uniform sample t-test to variable p.uniform and
# each normal sample t-test to variable p.normal.(use the c()-trick)
# In order to do so, you have to create two "outputvectors" p.uniform and p.normal
# before the loop. Also show me how you check after running the loop whether the lenght
# of each outputvector is as expected (50), so you know that you did things right.
# =========================================================================================
p.uniform = numeric()
p.normal = numeric()
for (i in 1:50){
unifdat = runif(100, -5, 5)
normdat = rnorm(100, 0, 5)
p.uniform = c(p.uniform, t.test(unifdat)$p.value)
p.normal = c(p.normal, t.test(normdat)$p.value)
}
length(p.uniform)
length(p.normal)
# =========================================================================================
## (2)
# Read in the textfile data_assignment11.txt that comes with this assignment and assign the
# resulting data.frame to a variable, say mydata.
# So, dim(mydata) gives 9 3. (otherwise you did something wrong).
# It should looks like the left matrix below. Now, change that data frame so that it looks
# exactly like the matrix on the right.
# A B C -> A B C
# 1 2 3 -> A 1 Doo 1 3three
# 4 5 6 -> A 4 Wee 2.5 6three
# 7 8 9 -> A 7 Doo 4 9three
# 10 11 12 -> A 10 Wee 5.5 12two
# 13 14 15 -> A 13 Doo 7 15two
# 16 17 18 -> A 16 Wee 8.5 18two
# 19 20 21 -> A 19 Doo 10 21one
# 22 23 24 -> A 22 Wee 11.5 24one
# 25 26 27 -> A 25 Doo 13 27one
#
# You need one line to read the file. Then another 3 (not more) lines to *paste* the
# values in m2 with the character strings that you want to add.
# =========================================================================================
m2 = read.table('data_assignment11.txt', header = T)
m2[,1] = paste('A', m2[,1])
m2[,2] = paste(c('Doo', 'Wee'), m2[, 2]/2)
m2[,3] = paste(m2[,3], rep(c('three','two','one'), each = 3), sep = '')
# =========================================================================================
## (3)
# a) Ask R: How many characters does each value in the C-column of m2 contain after the
# change you made? (very short line of code) If you don't know the name of the function you
# need to calculate the number of characters in each value, you could search for function
# names that contain "char" by typing apropos("char").
# b) Use indexing to simultaneously select the odd rows of both the B and C column. (there
# are many ways to do this, but you don't need %%)
# c) Show how you save the new object m2 to an Rdata file. (use save())
# =========================================================================================
# a)
nchar(mydata$C)
# b)
mydata[c(TRUE, FALSE), c('B', 'C')] # or
mydata[seq(1, 9, 2), 2:3]
# c)
save(mydata, file = 'some_name.Rdata')
# =========================================================================================
### Now, some new stuff.
## (4)
# Create a function CutHighLow in which you can put as arguments
# a vector of numbers (say, x), a minimum (say, min) and a maximum (say, max).
# The function should return all values of x that are higher than the minimum and lower
# than the maximum.
# =========================================================================================
CutHighLow = function(x, min, max){
out = x[x > min & x < max]
return(out)
}
# =========================================================================================
## (5)
# Below is a function called NewPlot that creates an empty plot. The function
# has default values for xlim and ylim of c(0, 1).
# Change the function slightly, so it passes all the eventual extra arguments on to plot().
NewPlot = function(xlim = c(0, 1), ylim = c(0, 1)){
plot(0, type = 'n', xlim = xlim, ylim = ylim)
}
# Try whether this works: Call the function and make the marks on the y-axis
# horizontal using an extra argument to your function.
# =========================================================================================
NewPlot = function(xlim = c(0, 1), ylim = c(0, 1), ...){
plot(0, type = 'n', xlim = xlim, ylim = ylim, ...)
}
NewPlot(las = 1)
# =========================================================================================
## (6) Challenge!
# Create your own function called which.divisible that takes as arguments n and divider.
# The function should tell you which of the numbers from 1 through n are divisible by
# the divider (in the sense that the division results in a round integer)
# =========================================================================================
which.divisible = function(n, divider){
test.vector = 1:n
divisible = test.vector %% divider == 0
test.vector[divisible]}
# =========================================================================================