All data in the exercises are simulated fake data.
<-
, ->
: assign values to objects.==
: check whether two values are equal.!=
: check whether two values are not equal.&
: and. Returns TRUE if both elements are TRUE.|
: or. Returns TRUE if one or both of the elements are TRUE.!
: not. Returns FALSE if statement is TRUE.:
: creates a series of numbers in a sequance. E.g. a <- c(1:10)
.%in%
: find out if an element belongs to a vector. E.g. x %in% y
.There are two ways to write the if statement in R: the standard if statement, and the ifelse()
function.
# If expression1 is true, statement1 will be executed. If expression1 is false but 2 is true, statement2 will be executed. Otherwise, statement3 will be executed.
if (expression1) {
statement1
} else if (expression2) {
statement2
} else {
statement3
}
# If expression is ture, statement1 will be executed, otherwise statement2 will be executed.
ifelse(expression, statement1, statement2)
In a while loop, test_expression
is evaluated and the statement is executed if the result is TRUE. Then the expression is evaluated again. A while loop will run repeatedly until text_expression
is no longer TRUE.
while (test_expression)
{
statement
}
In a for loop, value
takes on each element in the vector sequence
. statement
is executed in each iteration.
for (value in sequence)
{
statement
}
A teacher designed three sets of class materials (sets A, B, and C) for a language learning class. The teacher wants to know whether students’ learning outcome differ depending on the set of material used, and/or whether they receive a 10-min revision session after the class. 60 students participated in a round of trial classes: 10 students took part in the class with material set A, and received revision after class; 10 students also took part in the class with material set A, but did not receive revision; and so on.
This study is a 3 x 2 design with Material set (A vs. B vs. C) and Revision (revision vs. no revision).
Saying an affirmative sentence is true is easier than saying it’s false. For example, saying “A peguin is a bird” is true is easier (takes shorter time) than saying “A dolphin is a fish” is false. A researcher wants to know whether the same can be said about negative sentences.
10 participants took part in an experiment investigating negation processing. In each trial, the participant saw a visual display of a picture together with a sentence that is either congruent or incongruent with the visual display. Half of the sentences were affirmative, while the other half were negative. Participants were asked to jedge the truth value of each sentence. Their reaction time (seconds) was measured.1 Example adapted from Wang, S., Sun, C., Tian, Y., & Breheny, R. (2021). Verifying Negative Sentences. Journal of Psycholinguistic Research, 50(6), 1511-1534. Note that data used in this example is simulated and conclusions may differ from the real experiment.
This study has a 2 x 2 design with Polarity (affirmative vs. negative) and Truth value (true vs. false), resulting in four conditions. Each participant completed 15 trials in each condition.
data_frame$column_name <- as.factor(data_frame$column_name)
.data_mean <- aggregate(DV ~ factor1+factor2+factor3..., data=data_frame, FUN=mean)
.Suppose there is a language A, where sentences such as “Every girl in this class read her favorite book” can have two different interpretations: either (A) for each girl (e.g. \(x_1\), \(x_2\), \(x_3\),…) in the class, there is a book that she likes (e.g. \(x_1\) likes \(book_1\), \(x_2\) likes \(book_2\), \(x_3\) likes \(book_3\), etc.), and each girl read their respective favorite book; or (B) there is some girl/woman \(x\), such that each girl in this class read \(x\)’s favorite book.
A speaker of language A will wither consistently prefer one or the other of the two readings for this type of sentences. A linguist wants to know whether this kind of speaker variability depends on the region that the speaker is from (south vs. north). 100 native speakers (50 from the south, 50 from the north) read the example sentence and gave their preferred interpretation (A or B).