A psychologist has a theory that some of the negative cognitive effects of alcohol are the result of psychological rather than physiological processes. To test this, she has 16 participants perform a cognitive test before and after drinking non-alcoholic beer which was labelled to contain 5% alcohol. Results from the study, including some demographic data, are presented in the following table. Note that higher scores on the test indicate better performance.
| participant | before drink | after drink | age | sex | eyecolor |
|---|---|---|---|---|---|
| 1 | 45 | 43 | 20 | male | blue |
| 2 | 49 | 50 | 19 | female | blue |
| 3 | 40 | 61 | 22 | male | brown |
| 4 | 48 | 44 | 20 | female | brown |
| 5 | 44 | 45 | 27 | male | blue |
| 6 | 41 | 38 | 28 | female | blue |
| 7 | 52 | 45 | male | brown | |
| 8 | 52 | 40 | 19 | female | brown |
| 9 | 56 | 41 | 26 | male | blue |
| 10 | 52 | 48 | 23 | female | blue |
participant <- 1:10
before <- c(45, 49, 40, 48, 44, 41, 52, 52, 56, 52)
after <- c(43, 50, 61, 44, 45, 38, 45, 40, 41, 48)
age <- c(20, 19, 22, 20, 27, 28, NA, 19, 26, 23)
sex <- rep(x = c("male", "female"),
times = 5)
eyecolor <- rep(x = c("blue", "brown"),
each = 2,
length.out = 10)
avg.score <- (before + after) / 2
avg.score
## [1] 44.0 49.5 50.5 46.0 44.5 39.5 48.5 46.0 48.5 50.0
change <- after - before
change
## [1] -2 1 21 -4 1 -3 -7 -12 -15 -4
age.months <- age * 12
age.months
## [1] 240 228 264 240 324 336 NA 228 312 276
before <- before - 1
after <- after + 1
length(before)
## [1] 10
before.mean <- mean(before)
before.mean
## [1] 46.9
after.mean <- mean(after)
after.mean
## [1] 46.5
change.mean <- mean(after - before)
change.mean
## [1] -0.4
min(change)
## [1] -15
max(change)
## [1] 21
sd(age)
## [1] NA
# Oops! There is an NA value in the data,
# don't forget to include na.rm = T!
age.sd <- sd(age, na.rm = T)
age.sd
## [1] 3.535534
table(sex)
## sex
## female male
## 5 5
table(eyecolor)
## eyecolor
## blue brown
## 6 4
table(eyecolor) / sum(table(eyecolor))
## eyecolor
## blue brown
## 0.6 0.4
# OR
table(eyecolor) / length(eyecolor)
## eyecolor
## blue brown
## 0.6 0.4
table(age)
## age
## 19 20 22 23 26 27 28
## 2 2 1 1 1 1 1
table(age) / length(age)
## age
## 19 20 22 23 26 27 28
## 0.2 0.2 0.1 0.1 0.1 0.1 0.1
Pirates love putting their parrots into competitions. The following data represent results from two separate performance tasks given to five different parrots. In the first task, we measured how high the parrots could jump in centimeters (HIGHER scores are better). In the second task, we measured how many seconds it took for the parrots to fly around the ship 10 times (LOWER scores are better).
| parrot | jump | fly |
|---|---|---|
| Sebastiaan | 6.7 | 1085 |
| Thekla | 10.6 | 1081 |
| Sarina | 10.3 | 875 |
| Gavin | 13.7 | 725 |
| Kuebra | 8.9 | 1034 |
parrot <- c("Sebastian", "Thekla", "Sarina", "Gavin", "Kuebra")
jump <- c(6.7, 10.6, 10.3, 13.7, 8.9)
fly <- c(1085, 1081, 875, 725, 1034)
parrot <- c(parrot, "Seamus")
jump <- c(jump, 11.2)
fly <- c(fly, 920)
max(jump)
## [1] 13.7
# It was the fourth parrot -- Gavin
min(fly)
## [1] 725
# It was the fourth parrot -- Gavin again!
# Because larger values of fly are worse, I'll SUBTRACT
# fly from jump to calculate an average score. The final
# values might be negative, but I don't care -- I just
# need to find the largest (i.e.; least negative) value.
(jump - fly) / 2
## [1] -539.15 -535.20 -432.35 -355.65 -512.55 -454.40
max((jump - fly)/ 2)
## [1] -355.65
# Gavin again!
# Standardize both variables
jump.z <- (jump - mean(jump)) / sd(jump)
fly.z <- (fly - mean(fly)) / sd(fly)
# Calculate the average z-score (and still subtract fly.z)
avg.z <- (jump.z - fly.z) / 2
avg.z
## [1] -1.2219468 -0.3740620 0.2918612 1.5502777 -0.5709077 0.3247776
# Which is the max?
max(avg.z)
## [1] 1.550278
# Still Gavin -- maybe this wasn't the most exciting example..
sum(seq(from = 1, to = 578, by = 3) ^ .5)
## [1] 3092.179
a <- 50
a + 100
a
# It will return 50 because we didn't assign a again on the second line!