Click here to download this Rmd file so you can modify it yourself.
set.seed(2017-10-13)
library(tibble)
library(dplyr)
library(purrr)
library(knitr)
library(ggplot2)
# Making the fake data
data <- data_frame(Name1 = sample(c("Sean", "Emily", "Stephan"),
size = 100, replace = TRUE)) %>%
mutate(Name2 = map_chr(Name1, function(x) {
possible <- c("Sean", "Emily", "Stephan")
possible <- possible[possible != x]
sample(possible, 1)
})) %>%
mutate(Dyad = paste(Name1, Name2, sep = "+")) %>%
mutate(Session = rep(1:10, each = 10)) %>%
mutate(Action = sample(c("High-Five", "Shake-Hands", "Fist-Bump"),
size = 100, replace = TRUE))
# Let's peak at the data
data %>%
slice(1:10) %>%
kable()
Sean |
Stephan |
Sean+Stephan |
1 |
Shake-Hands |
Stephan |
Emily |
Stephan+Emily |
1 |
Fist-Bump |
Emily |
Stephan |
Emily+Stephan |
1 |
High-Five |
Stephan |
Emily |
Stephan+Emily |
1 |
Shake-Hands |
Stephan |
Sean |
Stephan+Sean |
1 |
Shake-Hands |
Sean |
Emily |
Sean+Emily |
1 |
High-Five |
Stephan |
Emily |
Stephan+Emily |
1 |
Shake-Hands |
Stephan |
Sean |
Stephan+Sean |
1 |
High-Five |
Emily |
Stephan |
Emily+Stephan |
1 |
Shake-Hands |
Emily |
Sean |
Emily+Sean |
1 |
Fist-Bump |
# Creating lagged actions by Session and Dyad
data <- data %>%
arrange(Session, Dyad) %>%
group_by(Session, Dyad) %>%
mutate(Prev_Action = lag(Action)) %>%
ungroup()
# Let's take a look at some slices of the data
data %>%
slice(c(1:5, 31:36, 71:76)) %>%
kable()
Emily |
Sean |
Emily+Sean |
1 |
Fist-Bump |
NA |
Emily |
Stephan |
Emily+Stephan |
1 |
High-Five |
NA |
Emily |
Stephan |
Emily+Stephan |
1 |
Shake-Hands |
High-Five |
Sean |
Emily |
Sean+Emily |
1 |
High-Five |
NA |
Sean |
Stephan |
Sean+Stephan |
1 |
Shake-Hands |
NA |
Emily |
Sean |
Emily+Sean |
4 |
High-Five |
NA |
Emily |
Stephan |
Emily+Stephan |
4 |
Fist-Bump |
NA |
Sean |
Stephan |
Sean+Stephan |
4 |
Shake-Hands |
NA |
Sean |
Stephan |
Sean+Stephan |
4 |
High-Five |
Shake-Hands |
Sean |
Stephan |
Sean+Stephan |
4 |
Fist-Bump |
High-Five |
Sean |
Stephan |
Sean+Stephan |
4 |
Shake-Hands |
Fist-Bump |
Emily |
Sean |
Emily+Sean |
8 |
High-Five |
NA |
Emily |
Sean |
Emily+Sean |
8 |
Fist-Bump |
High-Five |
Emily |
Sean |
Emily+Sean |
8 |
Shake-Hands |
Fist-Bump |
Emily |
Sean |
Emily+Sean |
8 |
High-Five |
Shake-Hands |
Emily |
Stephan |
Emily+Stephan |
8 |
High-Five |
NA |
Emily |
Stephan |
Emily+Stephan |
8 |
Shake-Hands |
High-Five |
# Now let's visualize it
data %>%
ggplot(aes(Dyad)) +
geom_bar(aes(fill = Action), position = "dodge")