library(flextable)
library(magrittr)
library(corrr)
library(tidyverse)
<- readRDS(file.path("..", "data", "hq-data.rds"))
data
<- readRDS("format.rds") formatAsTable
To build a correlation matrix of the variables, I first need to score the Likert scales for affective trust (aff.s
), cognitive trust (cog.s
), and liking (lik.s
).
<- data %>%
data mutate(across(c("cog4.int", "lik3.int", "lik4.int"),
~ 6 - .,
.names = "{.col}.r"))
<- data %>%
data rowwise(id) %>%
mutate(aff.s = mean(c(aff1.int, aff2.int,
aff3.int, aff4.int)),cog.s = mean(c(cog1.int, cog2.int,
cog3.int, cog4.int.r)),lik.s = mean(c(lik1.int, lik2.int,
%>%
lik3.int.r, lik4.int.r))) ungroup
Now I’ll give the correlation matrix the old college try.
<- data %>%
cor.tbl select(c(disclose, interdep, aff.s, cog.s, lik.s)) %>%
%>%
correlate
shave
%>%
cor.tbl formatAsTable
term | disclose | interdep | aff.s | cog.s | lik.s |
disclose | |||||
interdep | -0.03 | ||||
aff.s | 0.37 | 0.36 | |||
cog.s | 0.08 | -0.02 | 0.40 | ||
lik.s | 0.23 | 0.07 | 0.53 | 0.50 |
In APA style, two columns are added to the left-hand side of the matrix for means and standard deviations.
<- cor.tbl %>%
cor.tbl %>%
rowwise mutate(Mean = mean(data[[term]]),
"s.d." = sd(data[[term]]),
.before = disclose) %>%
ungroup
%>%
cor.tbl formatAsTable
term | Mean | s.d. | disclose | interdep | aff.s | cog.s | lik.s |
disclose | 0.52 | 0.50 | |||||
interdep | 0.49 | 0.50 | -0.03 | ||||
aff.s | 3.56 | 0.93 | 0.37 | 0.36 | |||
cog.s | 3.37 | 0.74 | 0.08 | -0.02 | 0.40 | ||
lik.s | 3.96 | 0.66 | 0.23 | 0.07 | 0.53 | 0.50 |
Finally, I need to rename the terms and format the values. APA style uses the natural numbers as headers.
<- c("Disclosure", "Interdependence",
varnames "Affective trust", "Cognitive trust",
"Liking") %>%
map2_chr(as.character(1:5), ~ paste0(.y, ". ", .x))
%>%
cor.tbl rename_with(~ as.character(1:4), disclose:cog.s) %>%
select(-term & -lik.s) %>%
add_column(Variable = varnames, .before = "Mean") %>%
mutate(across(as.character(1:4),
~ str_remove(str_remove(., "0(?=.)"),
"(?<=.\\d{2})\\d*"))) %>%
%>%
formatAsTable align(j = as.character(1:4), align = "right", part = "all")
Variable | Mean | s.d. | 1 | 2 | 3 | 4 |
1. Disclosure | 0.52 | 0.50 | ||||
2. Interdependence | 0.49 | 0.50 | -.03 | |||
3. Affective trust | 3.56 | 0.93 | .37 | .36 | ||
4. Cognitive trust | 3.37 | 0.74 | .08 | -.02 | .40 | |
5. Liking | 3.96 | 0.66 | .22 | .06 | .53 | .49 |
Save data:
<- function(data, name) {
saveData <- file.path("..", "data", name)
base saveRDS(data, paste0(base, ".rds"))
write_csv(data, paste0(base, ".csv"))
}
saveRDS(saveData, "save.rds")
%>%
data saveData("scored-data")
Output document:
options(knitr.duplicate.label = "allow")
::render("correlation.Rmd",
rmarkdownoutput_dir = file.path("..", "github", "thesis"))