Week 4: Learning Log
My Coding Goals this Week
My goals this week were to finish the dplyr modules, as well as starting coding for my paper.
Challenges and Successes
I started off with Danielle’s dplyr modules. It went smoothly initially but I kept making simple errors like typos in part 1 which frustrated me.
# install packages
install.packages("tidyverse")## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
install.packages("tidyselect")## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
# load packages
library(tidyverse)## ── Attaching packages ───────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 1.0.5
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ──────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(tidyselect)
# read the data
swow <- read_tsv("data_swow.csv.zip")## Multiple files in zip: reading 'swow.csv'
## Parsed with column specification:
## cols(
## cue = col_character(),
## response = col_character(),
## R1 = col_double(),
## N = col_double(),
## R1.Strength = col_double()
## )
swow <- swow %>% mutate(id = 1:n())
# renaming variables
swow <- swow %>%
rename(n_response = R1,
n_total = N,
strength = R1.Strength)
# filter the data (forward association)
woman_fwd <- swow %>%
filter(cue == "woman") %>%
filter(n_response > 1) %>%
select(cue, response, strength, id) %>%
mutate(
rank = rank(-strength),
type = "forward",
word = "woman",
associate = response
)
# making a plot
install.packages("ggplot2")## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(ggplot2)
ggplot(woman_fwd) + geom_col(mapping = aes(x = response, y = strength)) + coord_flip()# backward associations for woman
woman_bck <- swow %>%
filter(response == "woman",
n_response > 1) %>%
arrange(desc(strength)) %>%
select(cue, response, strength, id) %>%
mutate(
rank = rank(-strength),
type = "backwards",
word = "woman",
associate = cue
)
woman_bck## # A tibble: 200 x 8
## cue response strength id rank type word associate
## <chr> <chr> <dbl> <int> <dbl> <chr> <chr> <chr>
## 1 man woman 0.576 258593 1 backwards woman man
## 2 lady woman 0.36 240149 2 backwards woman lady
## 3 feminist woman 0.303 158641 3 backwards woman feminist
## 4 female woman 0.232 158492 4 backwards woman female
## 5 pregnant woman 0.18 327286 5 backwards woman pregnant
## 6 housewife woman 0.17 209394 6.5 backwards woman housewife
## 7 vagina woman 0.17 459047 6.5 backwards woman vagina
## 8 dame woman 0.167 105474 8 backwards woman dame
## 9 menopause woman 0.16 266238 9.5 backwards woman menopause
## 10 uterus woman 0.16 458533 9.5 backwards woman uterus
## # … with 190 more rows
# forward associations for man
man_fwd <- swow %>%
filter(cue == "man", n_response > 1) %>%
select(cue, response, strength, id) %>%
mutate(
rank = rank(-strength),
type = "forward",
word = "man",
associate = response
)
# plotting
ggplot(man_fwd) + geom_col(mapping = aes(x = response, y = strength)) + coord_flip()# backward associations for man
man_bck <- swow %>%
filter(response == "man",
n_response > 1) %>%
arrange(desc(strength)) %>%
select(-starts_with("n_")) %>%
mutate(
rank = rank(-strength),
type = "backward",
word = "man",
associate = cue
)
man_bck## # A tibble: 289 x 8
## cue response strength id rank type word associate
## <chr> <chr> <dbl> <int> <dbl> <chr> <chr> <chr>
## 1 macho man 0.51 256171 1 backward man macho
## 2 woman man 0.38 477315 2 backward man woman
## 3 handy man 0.333 195830 3 backward man handy
## 4 guy man 0.26 193254 4 backward man guy
## 5 super man 0.222 418318 5 backward man super
## 6 Mr man 0.22 277591 6 backward man Mr
## 7 bloke man 0.214 42923 7 backward man bloke
## 8 gingerbread man 0.202 181422 8 backward man gingerbread
## 9 fellow man 0.192 158313 9.5 backward man fellow
## 10 testosterone man 0.192 431406 9.5 backward man testosterone
## # … with 279 more rows
# bind the variables together
gender <- bind_rows(
man_bck, man_fwd, woman_bck, woman_fwd
) %>%
select(id:associate) %>%
filter(associate != "man", associate != "woman")
gender## # A tibble: 501 x 5
## id rank type word associate
## <int> <dbl> <chr> <chr> <chr>
## 1 256171 1 backward man macho
## 2 195830 3 backward man handy
## 3 193254 4 backward man guy
## 4 418318 5 backward man super
## 5 277591 6 backward man Mr
## 6 42923 7 backward man bloke
## 7 181422 8 backward man gingerbread
## 8 158313 9.5 backward man fellow
## 9 431406 9.5 backward man testosterone
## 10 210944 11 backward man hunk
## # … with 491 more rows
I made a few silly errors here (maybe I shouldn’t be coding late at night whoops) due to forgetting to capitalise letters, missing spaces and accidentally putting periods instead of commas. I also accidentally put %?% instead of %>%.
I’ve gotten into the habit of knitting my markdown doc together every few lines of code to better determine where I’m making errors, and catching them before they get too complicated!
Time for pivot!
# reading the file
love <- read_csv("data_love.csv")## Parsed with column specification:
## cols(
## colour = col_character(),
## heart = col_character(),
## book = col_character()
## )
# tibble
love## # A tibble: 4 x 3
## colour heart book
## <chr> <chr> <chr>
## 1 blue 💙 📘
## 2 green 💚 📗
## 3 yellow 💛 📒
## 4 orange 🧡 📙
# pivot to longer data
long_love <- love %>%
pivot_longer(
cols =c(heart, book),
names_to = "object",
values_to = "emoji"
)
long_love## # A tibble: 8 x 3
## colour object emoji
## <chr> <chr> <chr>
## 1 blue heart 💙
## 2 blue book 📘
## 3 green heart 💚
## 4 green book 📗
## 5 yellow heart 💛
## 6 yellow book 📒
## 7 orange heart 🧡
## 8 orange book 📙
# pivot to wider data
wide_love <- long_love %>%
pivot_wider(
id_cols = colour,
names_from = object,
values_from = emoji
)
wide_love## # A tibble: 4 x 3
## colour heart book
## <chr> <chr> <chr>
## 1 blue 💙 📘
## 2 green 💚 📗
## 3 yellow 💛 📒
## 4 orange 🧡 📙
# pivot forward associations
gender_fwd <- gender %>%
filter(type == "forward") %>%
pivot_wider(
id_cols = associate,
names_from = word,
values_from = rank
) %>%
mutate(
woman = replace_na(1/woman, 0),
man = replace_na(1/man, 0),
diff = woman - man
)
gender_fwd## # A tibble: 13 x 4
## associate man woman diff
## <chr> <dbl> <dbl> <dbl>
## 1 male 0.5 0 -0.5
## 2 human 0.333 0 -0.333
## 3 husband 0.25 0 -0.25
## 4 boy 0.154 0 -0.154
## 5 gender 0.154 0 -0.154
## 6 person 0.154 0 -0.154
## 7 strong 0.154 0.154 0
## 8 female 0 0.5 0.5
## 9 girl 0 0.333 0.333
## 10 lady 0 0.25 0.25
## 11 beauty 0 0.154 0.154
## 12 me 0 0.154 0.154
## 13 wife 0 0.154 0.154
ggplot(data = gender_fwd) +
geom_col(mapping = aes(
y = diff,
x = associate %>% reorder(diff)
)) +
coord_flip()The second part of these modules took me agesssss.
Some work from my paper..
install.packages("dplyr")## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(dplyr)
library(tidyverse)
library(haven)
# how many had covid/didnt have (thx Bart for this one!)
COVID <- read_sav(file ="coviddata.sav")
amount_covid <- COVID %>%
group_by(Ever_covid) %>%
count(Ever_covid)
amount_covid## # A tibble: 2 x 2
## # Groups: Ever_covid [2]
## Ever_covid n
## <dbl+lbl> <int>
## 1 0 [Think have not had coronavirus] 4656
## 2 1 [Think have had coronavirus] 1493
# immunity - no covid
strongagreeimmunenocovid <- COVID %>%
group_by(q8haveimmunity) %>%
filter(Ever_covid == 0, q8haveimmunity == 5) %>%
select(Ever_covid, q8haveimmunity) %>%
count(Ever_covid)
strongagreeimmunenocovid## # A tibble: 1 x 3
## # Groups: q8haveimmunity [1]
## q8haveimmunity Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 5 [Strongly agree] 0 [Think have not had coronavirus] 118
# immunity - covid
stronglyagreecovid <- COVID %>%
group_by(q8haveimmunity) %>%
filter(Ever_covid == 1, q8haveimmunity == 5) %>%
select(Ever_covid, q8haveimmunity) %>%
count(Ever_covid)
stronglyagreecovid## # A tibble: 1 x 3
## # Groups: q8haveimmunity [1]
## q8haveimmunity Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 5 [Strongly agree] 1 [Think have had coronavirus] 181
# grocery - no covid
grocerynocovid <- COVID %>%
group_by(Adhere_shop_groceries) %>%
filter(Ever_covid == 0, Adhere_shop_groceries == 1) %>%
select(Ever_covid, Adhere_shop_groceries) %>%
count(Ever_covid)
grocerynocovid## # A tibble: 1 x 3
## # Groups: Adhere_shop_groceries [1]
## Adhere_shop_groceries Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 1 [Once or less] 0 [Think have not had coronavirus] 2955
# grocery - covid
grocerycovid <- COVID %>%
group_by(Adhere_shop_groceries) %>%
filter(Ever_covid == 1, Adhere_shop_groceries == 1) %>%
select(Ever_covid, Adhere_shop_groceries) %>%
count(Ever_covid)
grocerycovid## # A tibble: 1 x 3
## # Groups: Adhere_shop_groceries [1]
## Adhere_shop_groceries Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 1 [Once or less] 1 [Think have had coronavirus] 805
# shopping - no covid
shoppingnocovid <- COVID %>%
group_by(Adhere_shop_other) %>%
filter(Ever_covid == 0, Adhere_shop_other == 1) %>%
select(Ever_covid, Adhere_shop_other) %>%
count(Ever_covid)
shoppingnocovid## # A tibble: 1 x 3
## # Groups: Adhere_shop_other [1]
## Adhere_shop_other Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 1 [Reported not shopping for non-essentials… 0 [Think have not had coro… 3500
# shopping - covid
shoppingcovid <- COVID %>%
group_by(Adhere_shop_other) %>%
filter(Ever_covid == 1, Adhere_shop_other == 1) %>%
select(Ever_covid, Adhere_shop_other) %>%
count(Ever_covid)
shoppingcovid## # A tibble: 1 x 3
## # Groups: Adhere_shop_other [1]
## Adhere_shop_other Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 1 [Reported not shopping for non-essentials (… 1 [Think have had corona… 816
# friends - no covid
friendsnocovid <- COVID %>%
group_by(Adhere_meet_friends) %>%
filter(Ever_covid == 0, Adhere_meet_friends == 0) %>%
select(Ever_covid, Adhere_meet_friends) %>%
count(Ever_covid)
friendsnocovid## # A tibble: 1 x 3
## # Groups: Adhere_meet_friends [1]
## Adhere_meet_friends Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 0 [Reported meeting friends / family from other… 0 [Think have not had … 456
# friends - covid
friendscovid <- COVID %>%
group_by(Adhere_meet_friends) %>%
filter(Ever_covid == 1, Adhere_meet_friends == 0) %>%
select(Ever_covid, Adhere_meet_friends) %>%
count(Ever_covid)
friendscovid## # A tibble: 1 x 3
## # Groups: Adhere_meet_friends [1]
## Adhere_meet_friends Ever_covid n
## <dbl+lbl> <dbl+lbl> <int>
## 1 0 [Reported meeting friends / family from other h… 1 [Think have had co… 422
Next Steps
My next steps are to continue coding the variables for each measure from our paper and figure out how to combine these into one code! I am working on the creating the graph and will try to work on the tables too but it seems very difficult!