Importing the Acitelli dataset into R.

acitelli_ind <- read.csv("/Users/randigarcia/Desktop/Three-day-workshop/R Workshop/Data/acitelli.csv", header=TRUE)

We ultimately want to get our data into the pairwise format for the APIM. We’ll need some more packages.

#install.packages("tidyr")
#install.packages("dplyr")

library(tidyr)
library(dplyr)

Individual to Dyad Structure

acitelli_dyd <- acitelli_ind %>% 
  mutate(gender = ifelse(gender == 1, "H", "W")) %>%
  gather(variable, value, self_pos:simhob) %>%
  unite(var_gender, variable, gender) %>%
  spread(var_gender, value)

head(acitelli_dyd)
##   cuplid  Yearsmar other_pos_H other_pos_W satisfaction_H satisfaction_W
## 1      3  8.202667         4.0         4.6       3.666667       4.000000
## 2     10 10.452667         4.0         3.8       3.666667       3.166667
## 3     11 -8.297333         4.8         4.4       3.833333       3.833333
## 4     17 -6.380667         4.4         3.6       3.833333       3.166667
## 5     21 10.202667         4.8         3.8       3.500000       4.000000
## 6     22 15.036000         4.6         5.0       4.000000       3.666667
##   self_pos_H self_pos_W simhob_H simhob_W tension_H tension_W
## 1        3.8        4.8        1        0       2.5       1.5
## 2        4.2        4.6        0        0       2.0       4.0
## 3        4.2        5.0        0        0       2.5       2.5
## 4        4.0        4.0        0       -1       2.0       3.0
## 5        4.4        4.2        0        0       2.5       3.5
## 6        4.4        4.0        0       -1       2.5       2.0

Individual to Pairwise Structure

tempA <- acitelli_ind %>% 
  mutate(genderE = gender, partnum = 1) %>%
  mutate(gender = ifelse(gender == 1, "A", "P")) %>%
  gather(variable, value, self_pos:genderE) %>%
  unite(var_gender, variable, gender) %>%
  spread(var_gender, value)

tempB <- acitelli_ind %>% 
  mutate(genderE = gender, partnum = 2) %>%
  mutate(gender = ifelse(gender == 1, "P", "A")) %>%
  gather(variable, value, self_pos:genderE)%>%
  unite(var_gender, variable, gender) %>%
  spread(var_gender, value)

acitelli_pair <- bind_rows(tempA, tempB) %>%
  arrange(cuplid) 
  
rm(tempA, tempB)
head(acitelli_pair)
##   cuplid  Yearsmar partnum genderE_A genderE_P other_pos_A other_pos_P
## 1      3  8.202667       1         1        -1         4.0         4.6
## 2      3  8.202667       2        -1         1         4.6         4.0
## 3     10 10.452667       1         1        -1         4.0         3.8
## 4     10 10.452667       2        -1         1         3.8         4.0
## 5     11 -8.297333       1         1        -1         4.8         4.4
## 6     11 -8.297333       2        -1         1         4.4         4.8
##   satisfaction_A satisfaction_P self_pos_A self_pos_P simhob_A simhob_P
## 1       3.666667       4.000000        3.8        4.8        1        0
## 2       4.000000       3.666667        4.8        3.8        0        1
## 3       3.666667       3.166667        4.2        4.6        0        0
## 4       3.166667       3.666667        4.6        4.2        0        0
## 5       3.833333       3.833333        4.2        5.0        0        0
## 6       3.833333       3.833333        5.0        4.2        0        0
##   tension_A tension_P
## 1       2.5       1.5
## 2       1.5       2.5
## 3       2.0       4.0
## 4       4.0       2.0
## 5       2.5       2.5
## 6       2.5       2.5