```{r directory}
# Windows
# setwd("C:/Users/(UserName)/Desktop/aol") 

# Mac
# setwd("/Users/(UserName)/Desktop/aol")

# A hashtag (#) works as a sign for R to ignore whatever's after it. To execute either setwd command above, simply remove the hashtag in the begining of the line. But be sure to delete the hash tag only in one of the two lines. If you're a Windows user, delete the hash tag before the Windows command. If you're a Mac user, the one before the Mac command. 
```

```{r library}
library(tidyverse)
library(pracma)
```

```{r data}
raw.data <- read.csv("sampleDat.csv")
```

```{r }
data0<-raw.data%>%
  gather(questions, responses, 2:16)%>%
  separate(questions, c("category", "Qnum"))%>%
  mutate(weight=case_when(
    Qnum==3 & responses ==1~0,
    Qnum==3 & responses ==2~1,
    Qnum==3 & responses ==3~10,
    Qnum==3 & responses ==4~50,
    Qnum==3 & responses ==5~250
  ))%>%
  mutate(number = case_when(
    Qnum ==1 ~"q1",
    Qnum ==2 ~"q2",
    Qnum ==3 ~"q3"
  ))%>%
  select(-Qnum)%>%
  group_by(Name, category)%>%
  fill(weight, .direction = "up")%>%
  spread(number, responses)

data1<-data0%>%
  select(Name, category, q1)%>%
  rename(partner=Name, partner_resp=q1)

data2<-inner_join(data0, data1, by="category")%>%
  filter(Name != partner)%>%
  mutate(partner_score = if_else(partner_resp > q2, c(1), c(partner_resp/q2)))%>%
  select(-c(q1, q2, q3, partner_resp))

data3<-data2%>% 
  mutate(joiner=paste(partner, Name, sep = "_"))

data2<-data2%>%
  mutate(joiner=paste(Name, partner, sep = "_"))

data4<-left_join(data2, data3, by = c("joiner", "category"))%>%
  rename(A = Name.x, B = Name.y, BonA = partner_score.x, AonB = partner_score.y, weight.A = weight.x, weight.B = weight.y)%>%
  select(A, B, category, AonB, BonA, weight.A, weight.B)%>%
  mutate(matchScore = ((weight.A*BonA + weight.B*AonB)/(weight.A+weight.B))*100)%>%
  select(A, B, category, matchScore)%>%
  spread(category, matchScore)%>%
  mutate(compatibility = nthroot(Appearance * Ethics * Intelligence * Kindness * Wealth, 5))

write.csv(data4, file = "comp.csv")
```
