Going Through the Exercises
Exercise 1
# load packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.4 ✓ purrr 0.3.4
## ✓ tibble 3.1.2 ✓ dplyr 1.0.6
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
# import data
swow <- "data_swow.csv.zip" %>%
read_tsv() %>% # read the data from file
mutate(id = 1:n()) %>% # add the "id" column
rename(
n_response = R1, # nicer name for the response count
n_total = N, # nicer name for the total cue presentations
strength = R1.Strength # nicer name for the estimated response strength
)
## ! Multiple files in zip: reading ''swow.csv''
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## cue = col_character(),
## response = col_character(),
## R1 = col_double(),
## N = col_double(),
## R1.Strength = col_double()
## )
Exercise 2 & 4 & 5 (Add Mutate)
# forward associates to the word "woman"
woman_fwd <- swow %>%
filter(cue == "woman", n_response > 1) %>%
select(cue, response, strength, id) %>% # select cue, response and strength
mutate(
rank = rank(-strength), #rank data by strength
type = "forward", # this is forward associate (i.e., it's woman_FWD)
word = "woman", # the word is "woman" (i.e., it's WOMAN_fwd)
associate = response # for forward associations, the RESPONSE is the asociate
)
# backward associates to the word "woman"
woman_bck <- swow %>%
filter (response == "woman", n_response >1) %>%
arrange(desc(strength)) %>%
select(-n_response, -n_total) %>% # select cue, response and strength
mutate(
rank = rank(-strength), # rank the data by strength
type = "backward", # this is backward associate (i.e., it's woman_BCK)
word = "woman", # the word is "woman" (i.e., it's WOMAN_bck)
associate = cue # for backward associations, the CUE is the associate
)
Exercise 3 & 5 (add Mutate)
# forward associates to the word "man"
man_fwd <- swow %>%
filter (cue == "man", n_response > 1) %>%
select(-n_response, -n_total) %>% # select cue, response and strength
mutate(
rank = rank(-strength), # rank the data by strength
type = "forward", # this is backward associate (i.e., it's woman_BCK)
word = "man", # the word is "woman" (i.e., it's WOMAN_bck)
associate = response # for backward associations, the CUE is the associate
)
# backward associates to the word "man"
man_bck <- swow %>%
filter (response == "man", n_response >1) %>%
arrange (desc(strength)) %>%
select(-n_response, -n_total) %>% # select cue, response and strength
mutate(
rank = rank(-strength), # rank the data by strength
type = "backward", # this is backward associate (i.e., it's woman_BCK)
word = "man", # the word is "woman" (i.e., it's WOMAN_bck)
associate = cue # for backward associations, the CUE is the associate
)
Exercise 5 Bind variables together
gender <- bind_rows (woman_fwd, woman_bck, man_fwd, man_bck) %>% #create "gender" using bind_rows()
select(id:associate) %>% #use select() to reduce the number of columns
filter(associate != "man", associate != "woman") #use filter() to remove the "boring" associations
Exercise 7 - Create and plot gender_bck
gender_bck <- gender %>%
filter(
type == "backward"
) %>%
pivot_wider(
id_cols = associate,
names_from = word,
values_from = rank
) %>%
mutate(
woman = (1/woman) %>% replace_na(0),
man = (1/man) %>% replace_na(0),
diff = woman - man
) %>%
arrange(diff)
picture_bck <- ggplot(
data = gender_bck %>% filter(diff < -0.05 | diff > 0.05),
mapping = aes(
x = associate %>% reorder(diff),
y = diff
)) +
geom_col(
) +
coord_flip()
plot(picture_bck)
