---
title: "R script data cleaning"
output:
  pdf_document:
    df_print: kable
    latex_engine: xelatex
---

```{r load packages, include=FALSE}
library(tidyverse)
library(psych)
library(base)
library(gmodels)
library(car)
library(reghelper)
library(rstatix)
```

```{r read raw dataset, echo=FALSE}
#The exact path needs to be adapted to load the data
RawData = read.csv(".../Data & Analyses/Raw Data.csv")

```

```{r clean dataset, echo=FALSE}
CleanData <- RawData %>%
  subset(Progress == 100) %>%
  select(-StartDate, -EndDate, -Progress, -Finished, -RecordedDate, -DistributionChannel, -UserLanguage, -InfoPage, -Status)


CleanData <- CleanData %>%
  mutate(MainOutcome = ifelse(MainOutcome == "7 (Completament d'acord)", 7, ifelse(MainOutcome == "1 (Molt en desacord)", 1, MainOutcome)),
         Item1 = ifelse(Item1 == "7 (Completament d'acord)", 7, ifelse(Item1 == "1 (Molt en desacord)", 1, Item1)),
         Item2 = ifelse(Item2 == "7 (Completament d'acord)", 7, ifelse(Item2 == "1 (Molt en desacord)", 1, Item2)),
         Item3 = ifelse(Item3 == "7 (Completament d'acord)", 7, ifelse(Item3 == "1 (Molt en desacord)", 1, Item3)),
         Item4 = ifelse(Item4 == "7 (Completament d'acord)", 7, ifelse(Item4 == "1 (Molt en desacord)", 1, Item4)),
         Item5 = ifelse(Item5 == "7 (Completament d'acord)", 7, ifelse(Item5 == "1 (Molt en desacord)", 1, Item5)),
         Item6 = ifelse(Item6 == "7 (Completament d'acord)", 7, ifelse(Item6 == "1 (Molt en desacord)", 1, Item6)),
         Item7 = ifelse(Item7 == "7 (Completament d'acord)", 7, ifelse(Item7 == "1 (Molt en desacord)", 1, Item7)),
         Item8 = ifelse(Item8 == "7 (Completament d'acord)", 7, ifelse(Item8 == "1 (Molt en desacord)", 1, Item8)),
         Item9 = ifelse(Item9 == "7 (Completament d'acord)", 7, ifelse(Item9 == "1 (Molt en desacord)", 1, Item9)),
         Item10 = ifelse(Item10 == "7 (Completament d'acord)", 7, ifelse(Item10 == "1 (Molt en desacord)", 1, Item10)),
         Item11 = ifelse(Item11 == "7 (Completament d'acord)", 7, ifelse(Item11 == "1 (Molt en desacord)", 1, Item11)),
         Item12 = ifelse(Item12 == "7 (Completament d'acord)", 7, ifelse(Item12 == "1 (Molt en desacord)", 1, Item12)),
         Item13 = ifelse(Item13 == "7 (Completament d'acord)", 7, ifelse(Item13 == "1 (Molt en desacord)", 1, Item13)),
         Item14 = ifelse(Item14 == "7 (Completament d'acord)", 7, ifelse(Item14 == "1 (Molt en desacord)", 1, Item14)),
         Item15 = ifelse(Item15 == "7 (Completament d'acord)", 7, ifelse(Item15 == "1 (Molt en desacord)", 1, Item15)),
         Item16 = ifelse(Item16 == "7 (Completament d'acord)", 7, ifelse(Item16 == "1 (Molt en desacord)", 1, Item16)),
         Item17 = ifelse(Item17 == "7 (Completament d'acord)", 7, ifelse(Item17 == "1 (Molt en desacord)", 1, Item17)),
         Item18 = ifelse(Item18 == "7 (Completament d'acord)", 7, ifelse(Item18 == "1 (Molt en desacord)", 1, Item18)),
         Item19 = ifelse(Item19 == "7 (Completament d'acord)", 7, ifelse(Item19 == "1 (Molt en desacord)", 1, Item19)),
         Item20 = ifelse(Item20 == "7 (Completament d'acord)", 7, ifelse(Item20 == "1 (Molt en desacord)", 1, Item20)),
         Item21 = ifelse(Item21 == "7 (Completament d'acord)", 7, ifelse(Item21 == "1 (Molt en desacord)", 1, Item21)),
         Item22 = ifelse(Item22 == "7 (Completament d'acord)", 7, ifelse(Item22 == "1 (Molt en desacord)", 1, Item22)),
         Item23 = ifelse(Item23 == "7 (Completament d'acord)", 7, ifelse(Item23 == "1 (Molt en desacord)", 1, Item23)),
         Item24 = ifelse(Item24 == "7 (Completament d'acord)", 7, ifelse(Item24 == "1 (Molt en desacord)", 1, Item24)),
         Item25 = ifelse(Item25 == "7 (Completament d'acord)", 7, ifelse(Item25 == "1 (Molt en desacord)", 1, Item25)),
         Item26 = ifelse(Item26 == "7 (Completament d'acord)", 7, ifelse(Item26 == "1 (Molt en desacord)", 1, Item26)),
         Item27 = ifelse(Item27 == "7 (Completament d'acord)", 7, ifelse(Item27 == "1 (Molt en desacord)", 1, Item27)),
         Item28 = ifelse(Item28 == "7 (Completament d'acord)", 7, ifelse(Item28 == "1 (Molt en desacord)", 1, Item28)),
         Item29 = ifelse(Item29 == "7 (Completament d'acord)", 7, ifelse(Item29 == "1 (Molt en desacord)", 1, Item29)),
         Item30 = ifelse(Item30 == "7 (Completament d'acord)", 7, ifelse(Item30 == "1 (Molt en desacord)", 1, Item30)),
         Item31 = ifelse(Item31 == "7 (Completament d'acord)", 7, ifelse(Item31 == "1 (Molt en desacord)", 1, Item31)),
         Item32 = ifelse(Item32 == "7 (Completament d'acord)", 7, ifelse(Item32 == "1 (Molt en desacord)", 1, Item32)),
         Item33 = ifelse(Item33 == "7 (Completament d'acord)", 7, ifelse(Item33 == "1 (Molt en desacord)", 1, Item33)),
         Item34 = ifelse(Item34 == "7 (Completament d'acord)", 7, ifelse(Item34 == "1 (Molt en desacord)", 1, Item34)),
         Item35 = ifelse(Item35 == "7 (Completament d'acord)", 7, ifelse(Item35 == "1 (Molt en desacord)", 1, Item35)),
         Item36 = ifelse(Item36 == "7 (Completament d'acord)", 7, ifelse(Item36 == "1 (Molt en desacord)", 1, Item36)))


CleanData$MainOutcome <- as.numeric(as.numeric(CleanData$MainOutcome))
CleanData$Item1 <- as.numeric(as.numeric(CleanData$Item1))
CleanData$Item2 <- as.numeric(as.numeric(CleanData$Item2))
CleanData$Item3 <- as.numeric(as.numeric(CleanData$Item3))
CleanData$Item4 <- as.numeric(as.numeric(CleanData$Item4))
CleanData$Item5 <- as.numeric(as.numeric(CleanData$Item5))
CleanData$Item6 <- as.numeric(as.numeric(CleanData$Item6))
CleanData$Item7 <- as.numeric(as.numeric(CleanData$Item7))
CleanData$Item8 <- as.numeric(as.numeric(CleanData$Item8))
CleanData$Item9 <- as.numeric(as.numeric(CleanData$Item9))
CleanData$Item10 <- as.numeric(as.numeric(CleanData$Item10))
CleanData$Item11 <- as.numeric(as.numeric(CleanData$Item11))
CleanData$Item12 <- as.numeric(as.numeric(CleanData$Item12))
CleanData$Item13 <- as.numeric(as.numeric(CleanData$Item13))
CleanData$Item14 <- as.numeric(as.numeric(CleanData$Item14))
CleanData$Item15 <- as.numeric(as.numeric(CleanData$Item15))
CleanData$Item16 <- as.numeric(as.numeric(CleanData$Item16))
CleanData$Item17 <- as.numeric(as.numeric(CleanData$Item17))
CleanData$Item18 <- as.numeric(as.numeric(CleanData$Item18))
CleanData$Item19 <- as.numeric(as.numeric(CleanData$Item19))
CleanData$Item20 <- as.numeric(as.numeric(CleanData$Item20))
CleanData$Item21 <- as.numeric(as.numeric(CleanData$Item21))
CleanData$Item22 <- as.numeric(as.numeric(CleanData$Item22))
CleanData$Item23 <- as.numeric(as.numeric(CleanData$Item23))
CleanData$Item24 <- as.numeric(as.numeric(CleanData$Item24))
CleanData$Item25 <- as.numeric(as.numeric(CleanData$Item25))
CleanData$Item26 <- as.numeric(as.numeric(CleanData$Item26))
CleanData$Item27 <- as.numeric(as.numeric(CleanData$Item27))
CleanData$Item28 <- as.numeric(as.numeric(CleanData$Item28))
CleanData$Item29 <- as.numeric(as.numeric(CleanData$Item29))
CleanData$Item30 <- as.numeric(as.numeric(CleanData$Item30))
CleanData$Item31 <- as.numeric(as.numeric(CleanData$Item31))
CleanData$Item32 <- as.numeric(as.numeric(CleanData$Item32))
CleanData$Item33 <- as.numeric(as.numeric(CleanData$Item33))
CleanData$Item34 <- as.numeric(as.numeric(CleanData$Item34))
CleanData$Item35 <- as.numeric(as.numeric(CleanData$Item35))
CleanData$Item36 <- as.numeric(as.numeric(CleanData$Item36))

#Reverse coding three items
CleanData <- CleanData %>%
  mutate(Item11 = 8 - Item11,
         Item19 = 8 - Item19,
         Item32 = 8-Item32)

CleanData <- CleanData %>%
  rename(Intention = MainOutcome,
         Knowledge1 = Item1,
         Skills1 = Item2,
         SPRI1 = Item3,
         BaCa1 = Item4,
         BaCa2 = Item5,
         BaCo1 = Item6,
         BaCo2 = Item7,
         BaCo3 = Item8,
         BaCo4 = Item9,
         MoGo1 = Item10,
         MoGo2 = Item11,
         Mem1 = Item12,
         Inn1 = Item13,
         Inn2 = Item14,
         Inn3 = Item15,
         SPC1 = Item16,
         SPC2 = Item17,
         Org1 = Item18,
         Org2 = Item19,
         Part1 = Item20,
         Part2 = Item21,
         Part3 = Item22,
         InnStr1 = Item23,
         InnStr2 = Item24,
         InnStr3 = Item25,
         InnStr4 = Item26,
         InnStr5 = Item27,
         InnStr6 = Item28,
         SI1 = Item29,
         SI2 = Item30,
         EmOp1 = Item31,
         EmOp2 = Item32,
         BR1 = Item33,
         BR2 = Item34,
         BR3 = Item35,
         NaBe1 = Item36,
         Gender = X1Gender,
         Age = X2Age,
         Country = X3Country,
         Location = X4LocationRvsU,
         Occupation = X5occupation,
         YearsPractice = X6YearsPractise,
         PracticeSetting = X7PractiseSetting,
         EverRecommended = X8EverRecommendedApp,
         PatientRequests = X9HowOftenPatientsAsk)


CleanData <- CleanData %>%
  mutate(Gender = ifelse(Gender == "Home", "Male", ifelse(Gender == "Dona", "Female", ifelse(Gender == "Prefereixo no respondre", "Prefer not to say", ifelse(Gender == "Altres", "Other", Gender)))))

CleanData <- CleanData %>%
  mutate(Age = ifelse(Age == "64 anys", 64, ifelse(Age == 500, NA, ifelse(Age == "", NA, Age))))
CleanData$Age <- as.numeric(as.numeric(CleanData$Age))

CleanData <- CleanData %>%
  mutate(Country = ifelse(Country == "A Catalunya", "Catalonia", "Other"))

CleanData <- CleanData %>%
  mutate(Location = ifelse(Location == "Urbana", "Urban", "Rural"))

CleanData <- CleanData %>%
  mutate(Occupation = ifelse(Occupation == "Infermera (sènior, júnior, o infermera en formació)", "Nurse", ifelse(Occupation == "Metge (sènior, júnior, o metge en formació)", "Physician", ifelse(Occupation == "Dentista", "Dentist", ifelse(Occupation == "Dietista registrat", "Registered dietitian", ifelse(Occupation == "Professional de la salut mental (psicòlegs, assessors o terapeutes)", "Mental health professional", ifelse(Occupation == "Altres", "Other", NA)))))))

CleanData <- CleanData %>%
  mutate(OccupationPhysician = ifelse(OccupationPhysician == "Anatomia Patològica", "Anatomical Pathology", ifelse(OccupationPhysician == "Anestesiologia", "Anesthesiology", ifelse(OccupationPhysician == "Cardiologia", "Cardiology", ifelse(OccupationPhysician == "Cirurgia Cardiovascular i toràcica", "Cardiovascular & Thoracic Surgery", ifelse(OccupationPhysician == "Cirurgia general", "General Surgery", ifelse(OccupationPhysician == "Cirurgia ortopèdica", "Orthopedic Surgery", ifelse(OccupationPhysician == "Cirurgia plàstica", "Plastic Surgery", ifelse(OccupationPhysician == "Dermatologia", "Dermatology", ifelse(OccupationPhysician == "Diagnòstic per la Imatge", "Diagnostic Radiology", ifelse(OccupationPhysician == "Endocrinologia i metabolisme", "Endocrinology and Metabolism", ifelse(OccupationPhysician == "Gastroentorologia", "Gastroenterology", ifelse(OccupationPhysician == "Hematologia", "Hematology", ifelse(OccupationPhysician == "Medicina Geriàtrica", "Geriatric Medicine", ifelse(OccupationPhysician == "Medicina Interna", "Internal Medicine", ifelse(OccupationPhysician == "Medicina Microbiologia i Malalties Infeccioses", "Medical Microbiology and Infectious Diseases", ifelse(OccupationPhysician == "Medicina Oncològica", "Medical Oncology", ifelse(OccupationPhysician == "Medicina de Cures Intensives", "Critical Care Medicine", ifelse(OccupationPhysician == "Medicina d’emergència", "Emergency Medicine", ifelse(OccupationPhysician == "Medicina familiar", "Family medicine", ifelse(OccupationPhysician == "Medicina física i rehabilitació", "Physical Medicine and Rehabilitation", ifelse(OccupationPhysician == "Medicina ocupacional", "Occupational Medicine", ifelse(OccupationPhysician == "Neurocirurgia", "Neurosurgery", ifelse(OccupationPhysician == "Neurologia", "Neurology", ifelse(OccupationPhysician == "Obstetrícia/Ginecologia", "Obstetrics/Gynecology", ifelse(OccupationPhysician == "Oftalmologia", "Ophthalmology", ifelse(OccupationPhysician == "Otolarologia", "Otolaryngology", ifelse(OccupationPhysician == "Patologia General/Clínica", "General/Clinical Pathology", ifelse(OccupationPhysician == "Pediatria", "Pediatrics", ifelse(OccupationPhysician == "Psiquiatria", "Psychiatry", ifelse(OccupationPhysician == "Respirologia", "Respirology", ifelse(OccupationPhysician == "Reumatologia", "Rheumatology", ifelse(OccupationPhysician == "Salut pública i medicina preventiva", "Public Health and Preventive Medicine", ifelse(OccupationPhysician == "Urologia", "Urology", ifelse(OccupationPhysician == "Altres", "Other", OccupationPhysician)))))))))))))))))))))))))))))))))))

CleanData <- CleanData %>%
  mutate(YearsPractice = ifelse(YearsPractice == "11 años", 11, ifelse(YearsPractice == "Más de 40", 40, ifelse(YearsPractice == "més de 40 anys", 40, ifelse(YearsPractice == "mes de 30", 30, ifelse(YearsPractice == "44 años", 44, ifelse(YearsPractice == "35 anys ", 35, ifelse(YearsPractice == "40 anys", 40, ifelse(YearsPractice == "si", NA, ifelse(YearsPractice == "cap", NA, ifelse(YearsPractice == "Si", NA, YearsPractice)))))))))))
CleanData$YearsPractice <- as.numeric(as.numeric(CleanData$YearsPractice))

CleanData <- CleanData %>%
  mutate(PracticeSetting = ifelse(PracticeSetting == "Centre de recerca acadèmica", "Research-Academic center", ifelse(PracticeSetting == "Centre de salut de la comunitat", "Community health center", ifelse(PracticeSetting == "Hospital: Ambulatori", "Hospital: Outpatient", ifelse(PracticeSetting == "Hospital: Pacient hospitalitzat", "Hospital: Inpatient", ifelse(PracticeSetting == "Pràctica assistencial primària", "Primary healthcare practice", ifelse(PracticeSetting == "Pràctica privada", "Private practice", ifelse(PracticeSetting == "Pràctica privada finançada públicament", "Private publicly funded practice", ifelse(PracticeSetting == "Altres", "Other", PracticeSetting)))))))))

CleanData <- CleanData %>%
  mutate(EverRecommended = ifelse(EverRecommended == "Sí", "Yes", ifelse(EverRecommended == "No n'estic segur", "Not sure", EverRecommended)))

CleanData <- CleanData %>%
  mutate(PatientRequests = ifelse(PatientRequests == "Mai", "Never", ifelse(PatientRequests == "Menys d'una vegada al mes", "Less than once per month", ifelse(PatientRequests == "Mensual", "Monthly", ifelse(PatientRequests == "Setmanalment", "Weekly", ifelse(PatientRequests == "Diàriament", "Daily", PatientRequests))))))

```

