Does drinking non-alcoholic beer affect cognitive performance?

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

Creating basic vectors

  1. Create a vector of the participant data but don’t use c(), use another vector creation function.
participant <- 1:10
  1. Create a vector of the before drink data
before <- c(45, 49, 40, 48, 44, 41, 52, 52, 56, 52)
  1. Create a vector of the after drink data
after <- c(43, 50, 61, 44, 45, 38, 45, 40, 41, 48)
  1. Create a vector of the age data. Set missing value to NA.
age <- c(20, 19, 22, 20, 27, 28, NA, 19, 26, 23)
  1. Create a vector of the sex data but don’t use c(), use another vector creation function.
sex <- rep(x = c("male", "female"), 
           times = 5)
  1. Create a vector of the eyecolor data but don’t use c(), use another vector creation function.
eyecolor <- rep(x = c("blue", "brown"),
                each = 2,
                length.out = 10)

Creating new vectors

  1. Create a new vector that shows the participants’ average score across both tests. (Hint: Don’t use the mean() function! Use basic math operations).
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
  1. Create a new vector that shows the change from before to after.
change <- after - before
change
##  [1]  -2   1  21  -4   1  -3  -7 -12 -15  -4
  1. Create a new vector that shows people’s age in months instead of years.
age.months <- age * 12
age.months
##  [1] 240 228 264 240 324 336  NA 228 312 276
  1. It turns out that the test results were mis-typed. All the before times are 1 point too high, and all the after times are 1 point too low. Correct them!
before <- before - 1
after <- after + 1

Applying functions to vectors

  1. What is the length of the before vector? Use the appropriate function!
length(before)
## [1] 10
  1. What was the mean before score? Assign the result to a scaler object with an appropriate name.
before.mean <- mean(before)
before.mean
## [1] 46.9
  1. What was the mean after score? Assign the result to a scaler object with an appropriate name.
after.mean <- mean(after)
after.mean
## [1] 46.5
  1. What was the mean difference in score-changes? Assign the result to a scaler object with an appropriate name.
change.mean <- mean(after - before)
change.mean
## [1] -0.4
  1. What was the maximum and minimum change?
min(change)
## [1] -15
max(change)
## [1] 21
  1. What was the standard deviation of ages? Assign the result to a scaler object with an appropriate name.
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
  1. How many people are there of each sex?
table(sex)
## sex
## female   male 
##      5      5
  1. How many people are there with each eye color?
table(eyecolor)
## eyecolor
##  blue brown 
##     6     4
  1. What percent of people have each eye color?
table(eyecolor) / sum(table(eyecolor))
## eyecolor
##  blue brown 
##   0.6   0.4
# OR

table(eyecolor) / length(eyecolor)
## eyecolor
##  blue brown 
##   0.6   0.4
  1. How many people had each age?
table(age)
## age
## 19 20 22 23 26 27 28 
##  2  2  1  1  1  1  1
  1. What percent of people had each age?
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

A parrot competition

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
  1. Assign the data to appropriately named objects.
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)
  1. Oops! We forgot to add the data for Seamus. Seamus jumped 11.2 centimeters and took 920 seconds to fly around the ship 10 times. Add Seamus’ data to the vectors.
parrot <- c(parrot, "Seamus")
jump <- c(jump, 11.2)
fly <- c(fly, 920)
  1. Which parrot did the best in the jumping task? To answer this, just calculate the appropriate summary statistic and visually look at which parrot had this score.
max(jump)
## [1] 13.7
# It was the fourth parrot -- Gavin
  1. Which parrot did the best in the flying task? Don’t forget that lower scores are better!
min(fly)
## [1] 725
# It was the fourth parrot -- Gavin again!
  1. Without standardizing the variables, which parrot did the best on average across both tasks?
# 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!
  1. After standardizing the variables, which parrot did the best on average across both tasks?
# 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..

Random…

  1. What is the sum of the square root of every third integer between 1 and 578?
sum(seq(from = 1, to = 578, by = 3) ^ .5)
## [1] 3092.179
  1. What will the following code return after the third line?
a <- 50
a + 100
a

# It will return 50 because we didn't assign a again on the second line!