Professional Documents
Culture Documents
Control Structures
1. For-loop.
2. Ifelse.
The following data was collected in an experiment with seeds of oats. The seeds were split
into three groups. Group 1 and 2 were separately treated by mordanting with the same
agent. Group 3 remained untreated (control). Seven seeds were taken from each group and
planted in 21 pots. At the end of the experiment, the yield per pot was measured (in grams).
Load the data from
url <- "http://stat.ethz.ch/Teaching/Datasets/WBL/hafer.dat"
d.hafer <- read.table(url, header = TRUE)
a) Using the variable YIELD, create a new categorial variable class consisting of 4 groups.
Hint: Take quartile breaks as cutting points.
b) Construct a new categorical variable which levels have the following properties:
1: YIELD < 350 and GROUP = 1, 3
2: YIELD > 350 and GROUP = 1, 3
3: YIELD < 350 and GROUP = 2
4: YIELD > 350 and GROUP = 2
c) Make contingency tables of your new variables and check the number of observations
for the different levels.
Hint: Use the command table().
3. Loops.
Functions
4. Writing a Function.
The goal is to write a function my.confint() that calculates the mean and approximate
confidence intervals for a vector of data. The lower and upper limits of the 95% confidence
interval of the estimated mean X̄ can be approximated as:
√ √ √
X̄ ± 2 · S/ n = X̄ − 2 · S/ n , X̄ + 2 · S/ n ,
Note: This approximation only applies if the distribution of X is not too skewed and n is not much
smaller than 10.
a) Think about the number and names of all necessary arguments for your function and
define your function name.
b) Make sure your function also works when it gets a vector containing missing values.
c) Write the body of your function which calculates the mean and the standard deviation.
d) Collect the result in a named output vector. The vector should return 3 values: the
mean, the lower end of the confidence interval, and the upper end of the confidence
interval.
e) Now try your function with a test vector, e.g.
c(2, 5, 2, 7, 3, 4, 7, 2, 9, 10, 1),
which yields mean 4.73, lower limit 2.86 and upper limit 6.60. Is this estimated mean
significantly different from zero?
5. if - else
Look at the following small function tst() which computes a qualifying string for a single
number x.
3