<- c(1, 0, 1, 1, 0)
accuracy <- c(520, 700, 480, 510, 730)
reaction_times == 1] reaction_times[accuracy
Exercises
Overview
Below you can find some practice problems that you can use to test your understanding of programming logic. For each question, first try to guess what the code will produce. Then see if you are correct by running the code on your own.
Exercises
1: Indexing
2: If-else
<- c("A", "B", "A", "B", "A")
condition <- ifelse(condition == "A", "face", "scene")
stimulus stimulus
3: Indexing
<- c("A", "B", "A", "B", "A")
condition <- c(1, 1, 0, 1, 1)
correct == 1 & condition == "A"] condition[correct
4: For loops
<- c(500, 480, 530)
rt1 <- c(520, 490, 500)
rt2 <- numeric(length(rt1))
diffs for (i in 1:length(rt1)) {
<- rt2[i] - rt1[i]
diffs[i]
} diffs
5: While loops
<- c(1, 1, 0, 1, 0, 1, 1)
responses <- 1
i <- 0
count while (responses[i] == 1) { # pay attention here to what will make the while loop stop
<- count + 1
count <- i + 1
i
} count
6: For loops and if-else
<- c(480, 610, 700)
rt <- character(length(rt))
labels for (i in 1:length(rt)) {
if (rt[i] < 500) {
<- "fast"
labels[i] else if (rt[i] <= 650) {
} <- "medium"
labels[i] else {
} <- "slow"
labels[i]
}
} labels
7: For loops and if-else
<- c(350, 560, 720, 450)
rt <- rep(FALSE, length(rt))
flags for (i in 1:length(rt)) {
if (rt[i] > 500) {
if (rt[i] < 700) {
<- TRUE
flags[i]
}
}
} flags
8: Data manipulation
<- data.frame(
df subject = c(1, 1, 2, 2),
condition = c("congruent", "incongruent", "congruent", "incongruent"),
rt = c(450, 600, 470, 620)
)
<- df %>%
df.new filter(condition == "incongruent")
df.new
9: Data manipulation
<- data.frame(
df trial = 1:5,
response = c("old", "new", "old", "old", "new"),
correct = c(TRUE, TRUE, FALSE, TRUE, FALSE)
)
<- df %>%
df.new mutate(score = ifelse(correct, 1, 0))
sum(df.new$score)
10: Functions
<- function(rt) {
categorize_rt if (is.na(rt)) {
return("missing")
else if (rt < 500) {
} return("fast")
else {
} return("slow")
}
}
categorize_rt(480)
11: Data manipulation
<- data.frame(
df trial = 1:5,
emotion = c("happy", "sad", "angry", "happy", "sad"),
rating = c(5, 3, 4, 4, 2)
)
%>%
df filter(emotion == "happy") %>%
mutate(adjusted = rating + 1)
12: Data summaries
<- data.frame(
df subject = c(1, 1, 2, 2),
condition = c("easy", "hard", "easy", "hard"),
correct = c(1, 0, 1, 1)
)
%>%
df group_by(condition) %>%
summarize(mean_acc = mean(correct))
13: Data manipulation
<- data.frame(
df trial = 1:4,
rt = c(500, 430, 670, 520)
)
%>%
df arrange(rt)
14: Data visualization
<- data.frame(
df condition = c("A","A","A","A","A","B","B","B","B","B"),
rt = c(420, 430, 510, 525, 415, 620, 510, 605, 430, 615)
)
ggplot(df, aes(x = condition, y = rt)) +
geom_boxplot()
15: Data visualization
<- data.frame(
df subject = c(1,2,3,4,5,1,2,3,4,5),
condition = c("easy","easy","easy","easy","easy","hard","hard","hard","hard","hard"),
accuracy = c(.8, 1, .75, .6, .8, .5, .7, .8, .4, .2)
)
ggplot(df, aes(x = condition, y = accuracy, color = factor(subject))) +
geom_point()