## assignment for lecture 5. To hand in before October 28th, 18.00
## (1)
# Say, I saved the exam.scores from 16 students in vector exam.scores:
# exam.scores = c(4.5, 5, 6, 4, 5, 3.5, 4, 6, 3, 6, 4.5, 5.5, 6, 5, 4, 2.5)
# make a for loop, that just prints these 16 scores, one at a time.
# (See first examples lecture 5)
# =========================================================================================
exam.scores = c(4.5, 5, 6, 4, 5, 3.5, 4, 6, 3, 6, 4.5, 5.5, 6, 5, 4, 2.5)
for (i in exam.scores){
print(i)
}
# =========================================================================================
## (2)
# Your answer above printed each exam score.
# Now edit this answer so that it only prints the score when it is lower than 4. Do so
# by including an if-statement. See last part lecture 5 for help.
# (You could get the answer much easier with exam.scores[exam.scores > 5],
# but this is an exercise to practice a "for" with "if" construction.)
# =========================================================================================
for (i in exam.scores){
if(i < 4){
print(i)
}
}
# =========================================================================================
## (3)
# Below, I create some fake data, containing the administration of an experiment.
# run: (and make sure you understand:)
subj = letters[1:12] # subjects with just the first letters of the alphabet as names
reward = c(2, 4, 3, 6, 2, 5, 3, 4, 2, 3, 4, 6) # what they earned in your experiment
forgotmoney = c(F, F, F, F, F, F, F, T, F, T, F, F) # oops, you forgot to pay some of them
admin.data = data.frame(subj, reward, forgotmoney)
# use head(admin.data) to check what it looks like
# Say, you want to calculate what you spent on rewards in your experiment.
# The easy way would be:
# sum(admin.data$reward[!admin.data$forgotmoney])
# But no, you like extra work and do it in a "for loop" over all the participants using
# an "if"-statement.
# I help you a bit with the code below, you only need to fill in the ...'s
total = 0 # start with a zero total:
for (i in 1:dim(admin.data)[1]){ # note: dim(admin.data)[1] is just the number of rows
if(admin.data$...[i] == ...){
total = total + admin.data$...[i]}
}
# type total to see if the result is 37 as it should.
# hint: you don't need the subj-variable.
# =========================================================================================
subj = letters[1:12]
reward = c(2, 4, 3, 6, 2, 5, 3, 4, 2, 3, 4, 6)
forgotmoney = c(F, F, F, F, F, F, F, T, F, T, F, F)
admin.data = data.frame(subj, reward, forgotmoney)
total = 0
for (i in 1:dim(admin.data)[1]){
if(admin.data$forgotmoney[i] == FALSE){
total = total + admin.data$reward[i]}
}
total
# =========================================================================================
## (4) Recap question:
# a) Logical indexing: Who are the subjects that you didn't pay? (of course, use R-code
# to tell the answer)
# b) Making a table: Make a frequency table (table()) that show how many got pay and how
# many didn't
# c) Logical indexing: What is the mean of the rewards that are higher than 3
# d) Integration of some tricks and indexing: Which participants got an even reward?
# e) And what were those rewards?
# f) Use numeric indexing to print admin.data starting from the bottom row. So, first,
# admin.data[12, ], then admin.data[11, ], and so forth.
# g) Select from admin.data only the subject name and the forgotmoney variable,
# for those subjects who had a reward higher than 3.
# h) Add a column to the admin.data called "best" that contains 1 for all subjects who
# scored higher than 3, 0 otherwise. The easiest way to go is to first add a column of
# zero's and then change some to one's.
# =========================================================================================
# a)
admin.data$subj[admin.data$forgotmoney]
# b)
table(admin.data$forgotmoney)
# c)
mean(admin.data$reward[admin.data$reward > 3])
# d)
admin.data$subj[admin.data$reward %% 2 == 0]
# e)
admin.data$reward[admin.data$reward %% 2 == 0]
# f)
admin.data[12:1, ]
# g)
admin.data[admin.data$reward > 3, c('subj', 'forgotmoney')]
# h)
admin.data$best = 0
admin.data$best[admin.data$reward > 3] = 1
# =========================================================================================
## (5) Challenge question (so, if you like)
# Load the data set data_lectre_5.Rdata in the lecture materials. You can do this by just
# double clicking it or coding: load('data_lecture_5.Rdata') when you're in the right
# directory.
# Now, do, for (yes, *for*) each participant a t-test comparing response time (RT) between
# correct and error responses. From each t-test, you want to save the p-value in "pvalues".
# To do so, create the for loop below and fill in the ... and replace <> by the
# proper commands:
# pvalues = numeric()
# for (subj in unique(d$subj)){ #unique gives you the different values in d$sbuj
# << select the data of a subject subj and assign to, e.g., tempd >>
# << compare RT of tempd between correct and error, saying t.out = t.test(...) >>
# << take pvalue from t.out and put in pvalues by
# saying pvalues = c(pvalues, t.out$p.value) >>
# }
# Is there a "significant" p-value for any of the participants?
# =========================================================================================
pvalues = numeric()
for (subj in unique(d$subj)){ #unique gives you the different values in d$sbuj
tempd = d[d$subj == subj,]
t.out = t.test(tempd$RT ~ tempd$correct)
pvalues = c(pvalues, t.out$p.value)}
# =========================================================================================