These questions are based on the lesson Analysing patient data from Programming with R.
Question 1
Consider the following dataframe (dat):
A | B | C | D | E |
5 | 24 | 53 | 23 | 78 |
2 | 102 | 57 | 1 | 34 |
7 | 38 | 2 | 149 | 246 |
3 | 48 | 6 | 46 | 2 |
12 | 1 | 301 | 3 | 58 |
Which code would correctly return the value 38.
dat[3, 2]
dat[c(3, 2)]
dat[2, 3]
dat[3:2]
Question 2
Which code would give a mean value for each of the rows in dat
apply(dat, 1, mean)
apply(dat, 2, mean)
rowMeans(dat)
- 1 and 3
- 2 and 3
Answers to question 2:
- Student does not realise that
rowMeans(dat)
does the same asapply(dat, 1, mean)
. - Student has mixed the value for rows and columns in the
MARGIN
argument, and has not consideredrowMeans
. - Student does not realise that
apply(dat, 1, mean)
also retrieves the row means. - Correct answer.
- Student has mixed the value for rows and columns in the
MARGIN
argument.