
#STUDY: VOICE SURVEY NURSING HOMES

#to clean the memory 
rm(list=ls())

#install.packages("dplyr")
#install.packages("tidyverse")
#install.packages("rstatix")
#install.packages("writexl")
#install.packages("readxl")

#Libraries
library(haven) # for dealing with labelled dataframe
library(tidyverse) # for data handling
library(rstatix) # for t-tests
library(dplyr) # to order the dataset
library(writexl) #to export as an excel file (the subdataset for coding)
library(readxl) #to read excel files

#Read survey data

#We worked with a dataset including an ID, however for data protection reasons, we share the version without ID
#this allows to reproduce most of the steps, except for some corrections based on respondents' ID 
#and the merging of the main dataset with the coded answers (code "2-Datapreparation-after coding")
#therefore the dataset that is obtained after this script and the one after merging are also provided in OSF 

path <- "yourpath" #Replace "yourpath" with the place where the data is saved
dataall=read_rds(file.path(path,"initial_dataallcomplete.rds"))

#to run most of the scripts, you can use the dataset without ID:
#dataall = read_rds(file.path(path,"initial_dataallcompletenoID.rds"))

dataall = as_factor(dataall)


#######################################################################
#Create functions to simplify the code later
########################################################################

#Function to get the n and proportions
prop=function(data, var){
  data %>% 
    drop_na({{var}}) %>%
    group_by({{var}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f))
}

#Function for proportions but by group
propby=function(data, var1, var2){
  data %>% 
    drop_na({{var1}}, {{var2}}) %>%
    group_by({{var1}}, {{var2}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f))
}
propby(dataall, GENDER, GRUPO)

######################################################################################################################################
#WHYTRANSP: create a new group variable that differentiates what they finally did (including the follow up) 
#####################################################################################################################################

#Control -> control_answer control_noanswer control_abandon
#Choice -> divide into Choice_textonly Choice_recordonly Choice_dictateonly Choice_several Choice_noanswer Choice_abandon
#PushDictation -> divide into PushDictation_dictated and PushDictation_nodictation
#PushRecording -> divide into PushRecording_recorded and PushRecording_norecording
#In addition, should include the follow up, since for PushDictation and PushRecording groups respondents might have answered in the follow up if not at time 1
#so divide into PushDictation_dictated1 PushDictation_dictated2 PushDictation_text2 PushDictation_nodictation PushDictation_abandon (similar for Recording)

#Create first a variable that is a dummy indicating if they wrote some text or not
dataall = dataall %>% mutate(
  WHYTRANSP_EXP_TEXTBOX = na_if(WHYTRANSP_EXP_TEXTBOX,"."), #Replace . by NA
  WHYTRANSP_EXP_TEXT = !is.na(WHYTRANSP_EXP_TEXTBOX), # New variable is TRUE is not NA
  WHYTRANSP_FOLLOW_TEXTBOX = na_if(WHYTRANSP_FOLLOW_TEXTBOX,"."),#same for follow up
  WHYTRANSP_FOLLOW_TEXT = !is.na(WHYTRANSP_FOLLOW_TEXTBOX)
) 
#Check
prop(dataall, WHYTRANSP_EXP_TEXT) 
prop(dataall, WHYTRANSP_FOLLOW_TEXT)

#Replace the NA by false in WHYTRANSP_EXP_Grabacion_1 (use to identify if respondents recorded their voice) to have only 2 values (true or false)
#Same in follow up
dataall = dataall %>% mutate(
  WHYTRANSP_EXP_Grabacion_1 = replace_na(WHYTRANSP_EXP_Grabacion_1,"false"),
  WHYTRANSP_FOLLOW_Grabacion_1 = replace_na(WHYTRANSP_FOLLOW_Grabacion_1,"false"),
)

prop(dataall, WHYTRANSP_EXP_Grabacion_1) 
prop(dataall, WHYTRANSP_FOLLOW_Grabacion_1) 

#To identify people dictating, in the first question, they had to click to see the textbox, so as long as they have an answer, they have clicked the button
#so in this case, use WHYTRANSP_EXP_Dictado (dummy indicating if they provided an answer) but replace the NAs by false
#Same for follow up

dataall = dataall %>% mutate(
   WHYTRANSP_EXP_Dictado = replace_na(WHYTRANSP_EXP_Dictado,"false"),  
  WHYTRANSP_FOLLOW_Dictado = replace_na(WHYTRANSP_FOLLOW_Dictado,"false"),  
)

prop(dataall, WHYTRANSP_EXP_Dictado) 
prop(dataall, WHYTRANSP_FOLLOW_Dictado) 

#However, in the follow up, in order to differentiate those who directly typed in some text from those who dictated, need to consider also WHYTRANSP_FOLLOW_DICTAR_RECORDED 
#WHYTRANSP_FOLLOW_DICTAR_RECORDED tells how many times they clicked the dictation button (if any) --> numeric variable so first create a dummy

prop(dataall, WHYTRANSP_FOLLOW_DICTAR_RECORDED)

dataall = dataall %>%
  mutate(WHYTRANSP_FOLLOW_DICTAR_RECORDED2 = case_when(
    is.na(WHYTRANSP_FOLLOW_DICTAR_RECORDED) ~ "false",
    WHYTRANSP_FOLLOW_DICTAR_RECORDED == 0 ~ "false",
    WHYTRANSP_FOLLOW_DICTAR_RECORDED > 0 ~ "true",
    TRUE ~ NA
  ))

prop(dataall, WHYTRANSP_FOLLOW_DICTAR_RECORDED2)

#Need to identify also people who abandon to separate abandon from item non response
#Since the questions TRANSPARENT and TRUSTNH are compulsory, we know who was still in at this point (just before the experimental ones)

dataall = dataall %>%
  mutate(ABANDON_WHYTRANSP = case_when(
    is.na(TRUSTNH) & !is.na(TRANSPARENT) ~ "true",
    TRUE ~ NA
  )) %>% mutate(ABANDON_WHYTRANSP = replace_na(ABANDON_WHYTRANSP, "false"))

prop(dataall, ABANDON_WHYTRANSP)

#Create the variable with all groups including what happened in follow up for the groups having it
dataall = dataall %>%
  mutate(GRUPO2TRANSP = case_when(
    GRUPO == "Control" & WHYTRANSP_EXP_TEXT==TRUE ~ "Control_answer",
    GRUPO == "Control" & WHYTRANSP_EXP_TEXT!=TRUE & ABANDON_WHYTRANSP!="true" ~ "Control_noanswer",
    GRUPO == "Control" & WHYTRANSP_EXP_TEXT!=TRUE & ABANDON_WHYTRANSP=="true" ~ "Control_abandon",
    GRUPO == "PushDictation" & WHYTRANSP_EXP_Dictado=="true" ~ "PushDictation_dictated1",
    GRUPO == "PushDictation" & WHYTRANSP_FOLLOW_DICTAR_RECORDED2=="true" ~ "PushDictation_dictated2",
    GRUPO == "PushDictation" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRANSP_FOLLOW_Dictado=="true" ~ "PushDictation_text2",
    GRUPO == "PushDictation" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRANSP_FOLLOW_Dictado!="true" & ABANDON_WHYTRANSP!="true" ~ "PushDictation_nodictation",
    GRUPO == "PushDictation" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRANSP_FOLLOW_Dictado!="true" & ABANDON_WHYTRANSP=="true" ~ "PushDictation_abandon",
    GRUPO == "PushRecording" & WHYTRANSP_EXP_Grabacion_1=="true" ~ "PushRecording_recorded1",
    GRUPO == "PushRecording" & WHYTRANSP_FOLLOW_Grabacion_1=="true" & WHYTRANSP_FOLLOW_TEXT==FALSE ~ "PushRecording_recordedonly2",
    GRUPO == "PushRecording" & WHYTRANSP_FOLLOW_Grabacion_1!="true" & WHYTRANSP_FOLLOW_TEXT==TRUE ~ "PushRecording_textonly2",
    GRUPO == "PushRecording" & WHYTRANSP_FOLLOW_Grabacion_1=="true" & WHYTRANSP_FOLLOW_TEXT==TRUE ~ "PushRecording_several2",
    GRUPO == "PushRecording" & WHYTRANSP_EXP_Grabacion_1!="true" & WHYTRANSP_FOLLOW_Grabacion_1!="true" & WHYTRANSP_FOLLOW_TEXT==FALSE & ABANDON_WHYTRANSP!="true" ~ "PushRecording_noanswer",
    GRUPO == "PushRecording" & WHYTRANSP_EXP_Grabacion_1!="true" & WHYTRANSP_FOLLOW_Grabacion_1!="true" & WHYTRANSP_FOLLOW_TEXT==FALSE & ABANDON_WHYTRANSP=="true" ~ "PushRecording_abandon",
    GRUPO == "Choice" & WHYTRANSP_EXP_Dictado=="true" & WHYTRANSP_EXP_Grabacion_1!="true" & WHYTRANSP_EXP_TEXT==FALSE ~ "Choice_dictatedonly",
    GRUPO == "Choice" & WHYTRANSP_EXP_Grabacion_1=="true" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_EXP_TEXT==FALSE ~ "Choice_recordedonly",
    GRUPO == "Choice" & WHYTRANSP_EXP_TEXT==TRUE & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_EXP_Grabacion_1!="true" ~ "Choice_textonly",
    GRUPO == "Choice" & (WHYTRANSP_EXP_TEXT==TRUE & WHYTRANSP_EXP_Grabacion_1=="true") | (WHYTRANSP_EXP_TEXT==TRUE & WHYTRANSP_EXP_Dictado=="true") 
    | (WHYTRANSP_EXP_Grabacion_1=="true" & WHYTRANSP_EXP_Dictado=="true") ~ "Choice_several",
    GRUPO == "Choice" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_EXP_Grabacion_1!="true" & WHYTRANSP_EXP_TEXT==FALSE & ABANDON_WHYTRANSP!="true" ~ "Choice_noanswer",
    GRUPO == "Choice" & WHYTRANSP_EXP_Dictado!="true" & WHYTRANSP_EXP_Grabacion_1!="true" & WHYTRANSP_EXP_TEXT==FALSE & ABANDON_WHYTRANSP=="true" ~ "Choice_abandon",
    TRUE ~ NA
  ))

#to see if it is all ok and see how many we have in each subgroup
dataall %>%
  group_by(GRUPO, GRUPO2TRANSP,WHYTRANSP_FOLLOW_Dictado, WHYTRANSP_EXP_Grabacion_1, WHYTRANSP_EXP_TEXT, WHYTRANSP_FOLLOW_DICTAR_RECORDED2, WHYTRANSP_FOLLOW_Grabacion_1, WHYTRANSP_FOLLOW_TEXT, ABANDON_WHYTRANSP) %>%
  count() %>%
  print(n = 30)

#to see if it is all ok and see how many we have in each subgroup but focusing on completes or abandon only (excluding the 2 completeISo for quality reasons)
dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRANSP,WHYTRANSP_FOLLOW_Dictado, WHYTRANSP_EXP_Grabacion_1, WHYTRANSP_EXP_TEXT, WHYTRANSP_FOLLOW_DICTAR_RECORDED2, WHYTRANSP_FOLLOW_Grabacion_1, WHYTRANSP_FOLLOW_TEXT, ABANDON_WHYTRANSP) %>%
  count() %>%
  print(n = 30)

#We  have 2 cases in PushDictation_dictated1 also classified as abandon -> checking them
datastrangecase = dataall %>% 
  filter(GRUPO2TRANSP=="PushDictation_dictated1" & ABANDON_WHYTRANSP=="true")
#They both provided an answer to WHYTRANSP but then abandoned just after giving the answer, so count them as "answered" (so OK as it is)


#Generate one new variable with open answers for all respondents
################################################################

#Need to join the answers 1, 2 and 3 in only one in the cases where one answer is saved in several variables
#This is the case for long answers in Dictation (main question + follow up question)

dataall = dataall %>%
  mutate(CONCAT_WHYTRANSP_EXP_ANSWER = paste(WHYTRANSP_EXP_ANSWER_1, WHYTRANSP_EXP_ANSWER_2, WHYTRANSP_EXP_ANSWER_3, sep = " "))

dataall <- dataall %>%
  mutate(CONCAT_WHYTRANSP_FOLLOW_ANSWER = paste(WHYTRANSP_FOLLOW_ANSWER_1, WHYTRANSP_FOLLOW_ANSWER_2, WHYTRANSP_FOLLOW_ANSWER_3, sep = " "))

#This is also the case for recording (main question + follow up question), but in this case we have also possibly 2 recordings
#Merge all parts in only one variable, so if there are 2 audios, we'll have the text from the 2 audios in only one variable

dataall = dataall %>%
  mutate(CONCAT_WHYTRANSP_EXP_AUDIO = 
           paste(WHYTRANSPEXP_AUDIO_1_TEXTO_1, WHYTRANSPEXP_AUDIO_1_TEXTO_2, WHYTRANSPEXP_AUDIO_1_TEXTO_3, WHYTRANSPFOLLOW_AUDIO_2_TEXTO_1,WHYTRANSPFOLLOW_AUDIO_2_TEXTO_2, WHYTRANSPFOLLOW_AUDIO_2_TEXTO_3, sep = " "))

dataall = dataall %>%
  mutate(CONCAT_WHYTRANSP_FOLLOW_AUDIO 
         = paste(WHYTRANSPFOLLOW_AUDIO_1_TEXTO_1, WHYTRANSPFOLLOW_AUDIO_1_TEXTO_2, WHYTRANSPFOLLOW_AUDIO_1_TEXTO_3, WHYTRANSPFOLLOW_AUDIO_2_TEXTO_1, WHYTRANSPFOLLOW_AUDIO_2_TEXTO_2, WHYTRANSPFOLLOW_AUDIO_2_TEXTO_3, sep = " "))

#Now create a new variable that has all answers, depending on the experimental group, taking these answers from different variables

#Control group -> answers are saved in WHYTRANSP_EXP_TEXTBOX
#PushDictation -> answers in CONCAT_WHYTRANSP_EXP_ANSWER or CONCAT_WHYTRANSP_FOLLOW_ANSWER (created just before)
#PushRecording -> answers in CONCAT_WHYTRANSP_EXP_AUDIO or CONCAT_WHYTRANSP_FOLLOW_AUDIO (created just before)
#Choice -> if they used Dictation, in CONCAT_WHYTRANSP_EXP_ANSWER or CONCAT_WHYTRANSP_FOLLOW_ANSWER,
#if they used voice in CONCAT_WHYTRANSP_EXP_AUDIO or CONCAT_WHYTRANSP_FOLLOW_AUDIO , if they used text in WHYTRANSP_EXP_TEXTBOX

prop(dataall, GRUPO2TRANSP)

#problem in PushRecording_several2, where 2 persons in follow up both recorded voice and wrote text
#problem also in Choice group for 17 respondents -> Choice_several
#since these are only few cases, checked them manually. Important: ID needed for this part. 

dataallcheckpb<- dataall %>% 
  select(id_ea, GRUPO2TRANSP, WHYTRANSP_EXP_TEXTBOX, CONCAT_WHYTRANSP_EXP_ANSWER, WHYTRANSP_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRANSP_FOLLOW_ANSWER, CONCAT_WHYTRANSP_EXP_AUDIO, CONCAT_WHYTRANSP_FOLLOW_AUDIO)

#keep only those
dataallcheckpb <- dataallcheckpb %>%
  filter(GRUPO2TRANSP=="Choice_several" | GRUPO2TRANSP=="PushRecording_several2")

#We see that for respondents in Choice_several, in fact we have the same answers twice or only a full answer in TEXTBOX
#so we can keep WHYTRANSP_EXP_TEXTBOX in these cases
#for the 2 cases in the PushRecording_several2, one is the same in both places, and one is in WHYTRANSP_FOLLOW_TEXTBOX (while the other answer is a non valid one)

#so we can create the final variable as follows
dataall = dataall %>%
  mutate(WHYTRANSP_FINAL = case_when(
    GRUPO2TRANSP == "Control_answer" ~ WHYTRANSP_EXP_TEXTBOX,
    GRUPO2TRANSP == "Control_noanswer" ~ NA,
    GRUPO2TRANSP == "Control_abandon" ~ NA,
    GRUPO2TRANSP == "PushDictation_dictated1" ~ CONCAT_WHYTRANSP_EXP_ANSWER, 
    GRUPO2TRANSP == "PushDictation_dictated2" ~ CONCAT_WHYTRANSP_FOLLOW_ANSWER,
    GRUPO2TRANSP == "PushDictation_text2" ~ CONCAT_WHYTRANSP_FOLLOW_ANSWER,
    GRUPO2TRANSP == "PushDictation_nodictation" ~ NA, 
    GRUPO2TRANSP == "PushDictation_abandon" ~ NA,
    GRUPO2TRANSP == "PushRecording_noanswer" ~ NA,
    GRUPO2TRANSP == "PushRecording_abandon" ~ NA,
    GRUPO2TRANSP == "PushRecording_recorded1" ~ CONCAT_WHYTRANSP_EXP_AUDIO,
    GRUPO2TRANSP == "PushRecording_recordedonly2" ~ CONCAT_WHYTRANSP_FOLLOW_AUDIO,
    GRUPO2TRANSP == "PushRecording_textonly2" ~ WHYTRANSP_FOLLOW_TEXTBOX,
    GRUPO2TRANSP == "PushRecording_several2" ~ WHYTRANSP_FOLLOW_TEXTBOX,
    GRUPO2TRANSP == "Choice_dictatedonly" ~ CONCAT_WHYTRANSP_EXP_ANSWER,
    GRUPO2TRANSP == "Choice_recordedonly" ~ CONCAT_WHYTRANSP_EXP_AUDIO,
    GRUPO2TRANSP == "Choice_textonly" ~ WHYTRANSP_EXP_TEXTBOX,
    GRUPO2TRANSP == "Choice_noanswer" ~ NA,
    GRUPO2TRANSP == "Choice_abandon" ~ NA,
    GRUPO2TRANSP == "Choice_several" ~ WHYTRANSP_EXP_TEXTBOX,
    TRUE ~ NA
  ))

#create dataset to check
dataallcheck<- dataall %>% 
  select(id_ea, GRUPO2TRANSP, WHYTRANSP_EXP_TEXTBOX, CONCAT_WHYTRANSP_EXP_ANSWER, WHYTRANSP_FOLLOW_TEXTBOX, 
        CONCAT_WHYTRANSP_FOLLOW_ANSWER, CONCAT_WHYTRANSP_EXP_AUDIO, CONCAT_WHYTRANSP_FOLLOW_AUDIO, WHYTRANSP_FINAL)

#to see the dataset ordered by GRUPO2TRANSP
dataallcheck <- dataallcheck %>% 
  arrange (GRUPO2TRANSP)

#Important: ID needed for this part
#problematic case: BLC6M2W -> as "recorded" only but no answer, same for: BL4JAAE, 2HKB6CG -> 3 in choice group
#also pb for 22 respondents in recording group (in group "recording", but no answer in fact): UYXGQ5K, 5HWM9ND, H4DPER3, 6V9FVP6, 3LKWRQB, NXGADBX, WVZVJHT, MK9JV8Q , XK5DBJF, 	
#3HLDP92, 4KFD7Q3, H7FWTJE, 25J684U, 5CJR9VJ, T4L4NE4, 9DMECMS, N5ASDRV, RMSBH76, 6YYAJHW, BZAD9Q6, Q8Y5VAS, C9DSCS5

pbqrespondentschoice <- c("BLC6M2W", "BL4JAAE" , "2HKB6CG") #list of problematic respondents

for (respondent in pbqrespondentschoice) { #for each one
  row_index1 <- which(dataall$id_ea == respondent) #look for the index
  dataall$GRUPO2TRANSP[row_index1] <- "Choice_noanswer" #correct the group (since no answer available, put them as "noanswer")
}

#same now for respondents in PushRecording group coded as "recorded" but for whom we actually have no answer
pbqrespondentsrecording <- c("UYXGQ5K", "5HWM9ND", "H4DPER3", "6V9FVP6","3LKWRQB", "NXGADBX", "WVZVJHT", "MK9JV8Q" , "XK5DBJF","3HLDP92", "4KFD7Q3", "H7FWTJE", "25J684U", "5CJR9VJ", "T4L4NE4", "9DMECMS", "N5ASDRV", "RMSBH76", "6YYAJHW", "BZAD9Q6", "Q8Y5VAS", "C9DSCS5")

for (respondent in pbqrespondentsrecording) {
    row_index2 <- which(dataall$id_ea == respondent)
    dataall$GRUPO2TRANSP[row_index2] <- "PushRecording_noanswer"
}

#to see the count by group after corrections (excluding the 2 completeISo for quality reasons)
dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRANSP,WHYTRANSP_FOLLOW_Dictado, WHYTRANSP_EXP_Grabacion_1, WHYTRANSP_EXP_TEXT, WHYTRANSP_FOLLOW_DICTAR_RECORDED2, WHYTRANSP_FOLLOW_Grabacion_1, WHYTRANSP_FOLLOW_TEXT, ABANDON_WHYTRANSP) %>%
  count() %>%
  print(n = 30)

dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRANSP) %>%
  count() %>%
  print(n = 30)

#then we run again the part to create WHYTRANSP_FINAL (to have NAs there instead of just empty answers)

dataall = dataall %>%
  mutate(WHYTRANSP_FINAL = case_when(
    GRUPO2TRANSP == "Control_answer" ~ WHYTRANSP_EXP_TEXTBOX,
    GRUPO2TRANSP == "Control_noanswer" ~ NA,
    GRUPO2TRANSP == "Control_abandon" ~ NA,
    GRUPO2TRANSP == "PushDictation_dictated1" ~ CONCAT_WHYTRANSP_EXP_ANSWER, 
    GRUPO2TRANSP == "PushDictation_dictated2" ~ CONCAT_WHYTRANSP_FOLLOW_ANSWER,
    GRUPO2TRANSP == "PushDictation_text2" ~ CONCAT_WHYTRANSP_FOLLOW_ANSWER,
    GRUPO2TRANSP == "PushDictation_nodictation" ~ NA, 
    GRUPO2TRANSP == "PushDictation_abandon" ~ NA,
    GRUPO2TRANSP == "PushRecording_noanswer" ~ NA,
    GRUPO2TRANSP == "PushRecording_abandon" ~ NA,
    GRUPO2TRANSP == "PushRecording_recorded1" ~ CONCAT_WHYTRANSP_EXP_AUDIO,
    GRUPO2TRANSP == "PushRecording_recordedonly2" ~ CONCAT_WHYTRANSP_FOLLOW_AUDIO,
    GRUPO2TRANSP == "PushRecording_textonly2" ~ WHYTRANSP_FOLLOW_TEXTBOX,
    GRUPO2TRANSP == "PushRecording_several2" ~ WHYTRANSP_FOLLOW_TEXTBOX,
    GRUPO2TRANSP == "Choice_dictatedonly" ~ CONCAT_WHYTRANSP_EXP_ANSWER,
    GRUPO2TRANSP == "Choice_recordedonly" ~ CONCAT_WHYTRANSP_EXP_AUDIO,
    GRUPO2TRANSP == "Choice_textonly" ~ WHYTRANSP_EXP_TEXTBOX,
    GRUPO2TRANSP == "Choice_noanswer" ~ NA,
    GRUPO2TRANSP == "Choice_abandon" ~ NA,
    GRUPO2TRANSP == "Choice_several" ~ WHYTRANSP_EXP_TEXTBOX,
    TRUE ~ NA
  ))

#create dataset to check
dataallcheck<- dataall %>% 
  select(id_ea, GRUPO2TRANSP, WHYTRANSP_EXP_TEXTBOX, CONCAT_WHYTRANSP_EXP_ANSWER, WHYTRANSP_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRANSP_FOLLOW_ANSWER, CONCAT_WHYTRANSP_EXP_AUDIO, CONCAT_WHYTRANSP_FOLLOW_AUDIO, WHYTRANSP_FINAL)

#to see the dataset ordered by GRUPO2TRANSP
dataallcheck <- dataallcheck %>% 
  arrange (GRUPO2TRANSP)



######################################################################################################################################
##Doing the same for WHYTRUSTNH 
#####################################################################################################################################

#Create first a variable that is a dummy indicating if they wrote some text or not
dataall = dataall %>% mutate(
  WHYTRUSTNH_EXP_TEXTBOX = na_if(WHYTRUSTNH_EXP_TEXTBOX,"."), #Replace . by NA
  WHYTRUSTNH_EXP_TEXT = !is.na(WHYTRUSTNH_EXP_TEXTBOX), # New variable is TRUE is not NA
  WHYTRUSTNH_FOLLOW_TEXTBOX = na_if(WHYTRUSTNH_FOLLOW_TEXTBOX,"."),#same for follow up
  WHYTRUSTNH_FOLLOW_TEXT = !is.na(WHYTRUSTNH_FOLLOW_TEXTBOX)
) 

prop(dataall, WHYTRUSTNH_EXP_TEXT) 
prop(dataall, WHYTRUSTNH_FOLLOW_TEXT)

#Replace the NA by false in WHYTRUSTNH_EXP_Grabacion_1 (use to identify if respondents recorded their voice) and WHYTRUSTNH_EXP_Dictado and both follow up
dataall = dataall %>% mutate(
  WHYTRUSTNH_EXP_Grabacion_1 = replace_na(WHYTRUSTNH_EXP_Grabacion_1,"false"),
  WHYTRUSTNH_FOLLOW_Grabacion_1 = replace_na(WHYTRUSTNH_FOLLOW_Grabacion_1,"false"),
  WHYTRUSTNH_EXP_Dictado = replace_na(WHYTRUSTNH_EXP_Dictado,"false"),  
  WHYTRUSTNH_FOLLOW_Dictado = replace_na(WHYTRUSTNH_FOLLOW_Dictado,"false"),
)

prop(dataall, WHYTRUSTNH_EXP_Grabacion_1) 
prop(dataall, WHYTRUSTNH_FOLLOW_Grabacion_1) 
prop(dataall, WHYTRUSTNH_EXP_Dictado) 
prop(dataall, WHYTRUSTNH_FOLLOW_Dictado) 

#However, in the follow up, in order to differentiate those who directly typed in some text from those who dictated, need to consider also WHYTRUSTNH_FOLLOW_DICTAR_RECORDED 
#WHYTRUSTNH_FOLLOW_DICTAR_RECORDED tells how many times they clicked the dictation button (if any) --> numeric variable so first create a dummy

prop(dataall, WHYTRUSTNH_FOLLOW_DICTAR_RECORDED)

dataall = dataall %>%
  mutate(WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2 = case_when(
    is.na(WHYTRUSTNH_FOLLOW_DICTAR_RECORDED) ~ "false",
    WHYTRUSTNH_FOLLOW_DICTAR_RECORDED == 0 ~ "false",
    WHYTRUSTNH_FOLLOW_DICTAR_RECORDED > 0 ~ "true",
    TRUE ~ NA
  ))

prop(dataall, WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2)

#To identify also people who abandon to separate abandon from item non response
#previous question is TRUSTNH and is compulsory. Next question is COVIDNH-> not compulsory so we could have someone with NA corresponding missing value instead of abandon
#since only few cases, let's use this and then check manually the ones classified as abandon, in case some would be only missing on the COVID question

dataall = dataall %>%
  mutate(ABANDON_WHYTRUSTNH = case_when(
    is.na(COVIDNH) & !is.na(TRUSTNH) ~ "true",
    TRUE ~ NA
  ))%>% mutate(ABANDON_WHYTRUSTNH = replace_na(ABANDON_WHYTRUSTNH, "false"))

prop(dataall, ABANDON_WHYTRUSTNH)

#check manually if these are really abandon: create dataset with only these cases, and look at it
dataallcheckabandon <- dataall %>%
  filter(ABANDON_WHYTRUSTNH=="true") #these indeed are all people who abandon on WHYTRUSTNH (or FOLLOW) so OK


#Create the variable with all groups including what happened in follow-up for the groups having it
#Cannot do it exactly as before, because otherwise we include in "noanswer" people who in fact have abandoned the survey before this question
#so filter here only people who have answered at TRUSTNH (before since the assignation to a group was done just when answering the question, people abandoning before had no group)

dataall = dataall %>%
  mutate(GRUPO2TRUSTNH= case_when(
    is.na(TRUSTNH) ~ NA,
    GRUPO == "Control" & WHYTRUSTNH_EXP_TEXT==TRUE ~ "Control_answer",
    GRUPO == "Control" & WHYTRUSTNH_EXP_TEXT!=TRUE & ABANDON_WHYTRUSTNH!="true" ~ "Control_noanswer",
    GRUPO == "Control" & WHYTRUSTNH_EXP_TEXT!=TRUE & ABANDON_WHYTRUSTNH=="true" ~ "Control_abandon",
    GRUPO == "PushDictation" & WHYTRUSTNH_EXP_Dictado=="true" ~ "PushDictation_dictated1",
    GRUPO == "PushDictation" & WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2=="true" ~ "PushDictation_dictated2",
    GRUPO == "PushDictation" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRUSTNH_FOLLOW_Dictado=="true" ~ "PushDictation_text2",
    GRUPO == "PushDictation" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRUSTNH_FOLLOW_Dictado!="true" & ABANDON_WHYTRUSTNH!="true" ~ "PushDictation_nodictation",
    GRUPO == "PushDictation" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2!="true" & WHYTRUSTNH_FOLLOW_Dictado!="true" & ABANDON_WHYTRUSTNH=="true" ~ "PushDictation_abandon",
    GRUPO == "PushRecording" & WHYTRUSTNH_EXP_Grabacion_1=="true" ~ "PushRecording_recorded1",
    GRUPO == "PushRecording" & WHYTRUSTNH_FOLLOW_Grabacion_1=="true" & WHYTRUSTNH_FOLLOW_TEXT==FALSE ~ "PushRecording_recordedonly2",
    GRUPO == "PushRecording" & WHYTRUSTNH_FOLLOW_Grabacion_1!="true" & WHYTRUSTNH_FOLLOW_TEXT==TRUE ~ "PushRecording_textonly2",
    GRUPO == "PushRecording" & WHYTRUSTNH_FOLLOW_Grabacion_1=="true" & WHYTRUSTNH_FOLLOW_TEXT==TRUE ~ "PushRecording_several2",
    GRUPO == "PushRecording" & WHYTRUSTNH_EXP_Grabacion_1!="true" & WHYTRUSTNH_FOLLOW_Grabacion_1!="true" & WHYTRUSTNH_FOLLOW_TEXT==FALSE & ABANDON_WHYTRUSTNH!="true" ~ "PushRecording_noanswer",
    GRUPO == "PushRecording" & WHYTRUSTNH_EXP_Grabacion_1!="true" & WHYTRUSTNH_FOLLOW_Grabacion_1!="true" & WHYTRUSTNH_FOLLOW_TEXT==FALSE & ABANDON_WHYTRUSTNH=="true" ~ "PushRecording_abandon",
    GRUPO == "Choice" & WHYTRUSTNH_EXP_Dictado=="true" & WHYTRUSTNH_EXP_Grabacion_1!="true" & WHYTRUSTNH_EXP_TEXT==FALSE ~ "Choice_dictatedonly",
    GRUPO == "Choice" & WHYTRUSTNH_EXP_Grabacion_1=="true" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_EXP_TEXT==FALSE ~ "Choice_recordedonly",
    GRUPO == "Choice" & WHYTRUSTNH_EXP_TEXT==TRUE & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_EXP_Grabacion_1!="true" ~ "Choice_textonly",
    GRUPO == "Choice" & (WHYTRUSTNH_EXP_TEXT==TRUE & WHYTRUSTNH_EXP_Grabacion_1=="true") | (WHYTRUSTNH_EXP_TEXT==TRUE & WHYTRUSTNH_EXP_Dictado=="true") 
    | (WHYTRUSTNH_EXP_Grabacion_1=="true" & WHYTRUSTNH_EXP_Dictado=="true") ~ "Choice_several",
    GRUPO == "Choice" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_EXP_Grabacion_1!="true" & WHYTRUSTNH_EXP_TEXT==FALSE & ABANDON_WHYTRUSTNH!="true" ~ "Choice_noanswer",
    GRUPO == "Choice" & WHYTRUSTNH_EXP_Dictado!="true" & WHYTRUSTNH_EXP_Grabacion_1!="true" & WHYTRUSTNH_EXP_TEXT==FALSE & ABANDON_WHYTRUSTNH=="true" ~ "Choice_abandon",
    TRUE ~ NA
  ))


propby(dataall, WHYTRUSTNH_FOLLOW_Dictado, WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2)

#to see if it is all ok and see how many we have in each subgroup
dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRUSTNH,WHYTRUSTNH_FOLLOW_Dictado, WHYTRUSTNH_EXP_Grabacion_1, WHYTRUSTNH_EXP_TEXT, WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2, WHYTRUSTNH_FOLLOW_Grabacion_1, WHYTRUSTNH_FOLLOW_TEXT) %>%
  count() %>%
  print(n = 30)

#Generate one new variable with open answers for all respondents
################################################################

#Join the answers 1, 2 and 3 in only one in the cases where one answer is saved in several variables
#This is the case for long answers in Dictation (main question + follow up question)

dataall <- dataall %>%
  mutate(CONCAT_WHYTRUSTNH_EXP_ANSWER = paste(WHYTRUSTNH_EXP_ANSWER_1, WHYTRUSTNH_EXP_ANSWER_2, WHYTRUSTNH_EXP_ANSWER_3, sep = " "))

dataall <- dataall %>%
  mutate(CONCAT_WHYTRUSTNH_FOLLOW_ANSWER = paste(WHYTRUSTNH_FOLLOW_ANSWER_1, WHYTRUSTNH_FOLLOW_ANSWER_2, WHYTRUSTNH_FOLLOW_ANSWER_3, sep = " "))

#This is also the case for recording (main question + follow up question), but in this case we have also possibly 2 recordings
#Merge all parts in only one variable, so if there are 2 audios, we'll have the text from the 2 audios in only one variable

dataall <- dataall %>%
  mutate(CONCAT_WHYTRUSTNH_EXP_AUDIO = 
           paste(WHYTRUSTNHEXP_AUDIO_1_TEXTO_1, WHYTRUSTNHEXP_AUDIO_1_TEXTO_2, WHYTRUSTNHEXP_AUDIO_1_TEXTO_3, WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_1,WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_2, WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_3, sep = " "))

dataall <- dataall %>%
  mutate(CONCAT_WHYTRUSTNH_FOLLOW_AUDIO 
         = paste(WHYTRUSTNHFOLLOW_AUDIO_1_TEXTO_1, WHYTRUSTNHFOLLOW_AUDIO_1_TEXTO_2, WHYTRUSTNHFOLLOW_AUDIO_1_TEXTO_3, WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_1, WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_2, WHYTRUSTNHFOLLOW_AUDIO_2_TEXTO_3, sep = " "))


#Now create a new variable that has all answers, depending on the experimental group, taking these answers from different variables
#Control group -> answers are saved in WHYTRUSTNH_EXP_TEXTBOX
#PushDictation -> answers in CONCAT_WHYTRUSTNH_EXP_ANSWER or CONCAT_WHYTRUSTNH_FOLLOW_ANSWER (created just before)
#PushRecording -> answers in CONCAT_WHYTRUSTNH_EXP_AUDIO or CONCAT_WHYTRUSTNH_FOLLOW_AUDIO (created just before)
#Choice -> if they used Dictation, in CONCAT_WHYTRUSTNH_EXP_ANSWER or CONCAT_WHYTRUSTNH_FOLLOW_ANSWER,
#if they used voice in CONCAT_WHYTRUSTNH_EXP_AUDIO or CONCAT_WHYTRUSTNH_FOLLOW_AUDIO , if they used text in WHYTRUSTNH_EXP_TEXTBOX

prop(dataall, GRUPO2TRUSTNH)

#problem in Choice group for 5 respondents -> Choice_several
#since these are only few cases, check them manually 

dataallcheckpb2 <- dataall %>% 
  select(id_ea, GRUPO2TRUSTNH, WHYTRUSTNH_EXP_TEXTBOX, CONCAT_WHYTRUSTNH_EXP_ANSWER, WHYTRUSTNH_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRUSTNH_FOLLOW_ANSWER, CONCAT_WHYTRUSTNH_EXP_AUDIO, CONCAT_WHYTRUSTNH_FOLLOW_AUDIO)

#keep only those
dataallcheckpb2 <- dataallcheckpb2 %>%
  filter(GRUPO2TRUSTNH=="Choice_several")

#Respondents in Choice_several: 2 different answers in some cases, so join the 2 answers in the final variable 
#(concat de CONCAT_WHYTRUSTNH_EXP_ANSWER and CONCAT_WHYTRUSTNH_EXP_AUDIO)

dataall <- dataall %>%
  mutate(CONCAT_WHYTRUSTNH_CHOICE_SEVERAL 
         = paste(CONCAT_WHYTRUSTNH_EXP_ANSWER, CONCAT_WHYTRUSTNH_EXP_AUDIO, sep = " "))

#checking. ID needed for this part
dataallcheckpb2 <- dataall %>% 
  select(id_ea, GRUPO2TRUSTNH, WHYTRUSTNH_EXP_TEXTBOX, CONCAT_WHYTRUSTNH_EXP_ANSWER, WHYTRUSTNH_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRUSTNH_FOLLOW_ANSWER, CONCAT_WHYTRUSTNH_EXP_AUDIO, CONCAT_WHYTRUSTNH_FOLLOW_AUDIO, CONCAT_WHYTRUSTNH_CHOICE_SEVERAL)

dataallcheckpb2 <- dataallcheckpb2 %>%
  filter(GRUPO2TRUSTNH=="Choice_several") #ok

#we create the final variable as follows
dataall = dataall %>%
  mutate(WHYTRUSTNH_FINAL = case_when(
    GRUPO2TRUSTNH == "Control_answer" ~ WHYTRUSTNH_EXP_TEXTBOX,
    GRUPO2TRUSTNH == "Control_noanswer" ~ NA,
    GRUPO2TRUSTNH == "Control_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushDictation_dictated1" ~ CONCAT_WHYTRUSTNH_EXP_ANSWER, 
    GRUPO2TRUSTNH == "PushDictation_dictated2" ~ CONCAT_WHYTRUSTNH_FOLLOW_ANSWER,
    GRUPO2TRUSTNH == "PushDictation_text2" ~ CONCAT_WHYTRUSTNH_FOLLOW_ANSWER,
    GRUPO2TRUSTNH == "PushDictation_nodictation" ~ NA, 
    GRUPO2TRUSTNH == "PushDictation_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_noanswer" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_recorded1" ~ CONCAT_WHYTRUSTNH_EXP_AUDIO,
    GRUPO2TRUSTNH == "PushRecording_recordedonly2" ~ CONCAT_WHYTRUSTNH_FOLLOW_AUDIO,
    GRUPO2TRUSTNH == "PushRecording_textonly2" ~ WHYTRUSTNH_FOLLOW_TEXTBOX,
    GRUPO2TRUSTNH == "Choice_dictatedonly" ~ CONCAT_WHYTRUSTNH_EXP_ANSWER,
    GRUPO2TRUSTNH == "Choice_recordedonly" ~ CONCAT_WHYTRUSTNH_EXP_AUDIO,
    GRUPO2TRUSTNH == "Choice_textonly" ~ WHYTRUSTNH_EXP_TEXTBOX,
    GRUPO2TRUSTNH == "Choice_noanswer" ~ NA,
    GRUPO2TRUSTNH == "Choice_abandon" ~ NA,
    GRUPO2TRUSTNH == "Choice_several" ~ CONCAT_WHYTRUSTNH_CHOICE_SEVERAL,
    TRUE ~ NA
  ))

#create dataset to check
dataallcheckpb3<- dataall %>% 
  select(id_ea, GRUPO2TRUSTNH, WHYTRUSTNH_EXP_TEXTBOX, CONCAT_WHYTRUSTNH_EXP_ANSWER, WHYTRUSTNH_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRUSTNH_FOLLOW_ANSWER, CONCAT_WHYTRUSTNH_EXP_AUDIO, CONCAT_WHYTRUSTNH_FOLLOW_AUDIO, WHYTRUSTNH_FINAL)

#to see the dataset ordered by GRUPO2TRUSTNH
dataallcheckpb3 <- dataallcheckpb3 %>% 
  arrange (GRUPO2TRUSTNH)

#ID needed for this part
#Problem in Choice-recordedonly no answer for: XCEUSCR
#Problem in PushRecording_recorded1 no answer for: 6V9FVP6, 3LKWRQB, NXGADBX, 3HLDP92, 4KFD7Q3, 25J684U, 	
#5CJR9VJ, C9DSCS5, N5ASDRV, 7A9GDXU, RMSBH76, 6YYAJHW, BZAD9Q6, Q8Y5VAS 	
#PushRecording_recordedonly2: T4L4NE4

#replace values for problematic respondents

row_index3 <- which(dataall$id_ea == "XCEUSCR")
dataall$GRUPO2TRUSTNH[row_index3] <- "Choice_noanswer"

pbqrespondentsrecording1 <- c("6V9FVP6", "3LKWRQB", "NXGADBX", "3HLDP92", "4KFD7Q3", "25J684U", "5CJR9VJ", "C9DSCS5", "N5ASDRV", "7A9GDXU", "RMSBH76", "6YYAJHW", "BZAD9Q6", "Q8Y5VAS", "T4L4NE4")

for (respondent in pbqrespondentsrecording1) {
  row_index4 <- which(dataall$id_ea == respondent)
  dataall$GRUPO2TRUSTNH[row_index4] <- "PushRecording_noanswer"
}

#then run again this:
dataall = dataall %>%
  mutate(WHYTRUSTNH_FINAL = case_when(
    GRUPO2TRUSTNH == "Control_answer" ~ WHYTRUSTNH_EXP_TEXTBOX,
    GRUPO2TRUSTNH == "Control_noanswer" ~ NA,
    GRUPO2TRUSTNH == "Control_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushDictation_dictated1" ~ CONCAT_WHYTRUSTNH_EXP_ANSWER, 
    GRUPO2TRUSTNH == "PushDictation_dictated2" ~ CONCAT_WHYTRUSTNH_FOLLOW_ANSWER,
    GRUPO2TRUSTNH == "PushDictation_text2" ~ CONCAT_WHYTRUSTNH_FOLLOW_ANSWER,
    GRUPO2TRUSTNH == "PushDictation_nodictation" ~ NA, 
    GRUPO2TRUSTNH == "PushDictation_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_noanswer" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_abandon" ~ NA,
    GRUPO2TRUSTNH == "PushRecording_recorded1" ~ CONCAT_WHYTRUSTNH_EXP_AUDIO,
    GRUPO2TRUSTNH == "PushRecording_recordedonly2" ~ CONCAT_WHYTRUSTNH_FOLLOW_AUDIO,
    GRUPO2TRUSTNH == "PushRecording_textonly2" ~ WHYTRUSTNH_FOLLOW_TEXTBOX,
    GRUPO2TRUSTNH == "Choice_dictatedonly" ~ CONCAT_WHYTRUSTNH_EXP_ANSWER,
    GRUPO2TRUSTNH == "Choice_recordedonly" ~ CONCAT_WHYTRUSTNH_EXP_AUDIO,
    GRUPO2TRUSTNH == "Choice_textonly" ~ WHYTRUSTNH_EXP_TEXTBOX,
    GRUPO2TRUSTNH == "Choice_noanswer" ~ NA,
    GRUPO2TRUSTNH == "Choice_abandon" ~ NA,
    GRUPO2TRUSTNH == "Choice_several" ~ CONCAT_WHYTRUSTNH_CHOICE_SEVERAL,
    TRUE ~ NA
  ))

#check again
dataallcheckpb3<- dataall %>% 
  select(id_ea, GRUPO2TRUSTNH, WHYTRUSTNH_EXP_TEXTBOX, CONCAT_WHYTRUSTNH_EXP_ANSWER, WHYTRUSTNH_FOLLOW_TEXTBOX, 
         CONCAT_WHYTRUSTNH_FOLLOW_ANSWER, CONCAT_WHYTRUSTNH_EXP_AUDIO, CONCAT_WHYTRUSTNH_FOLLOW_AUDIO, WHYTRUSTNH_FINAL)

#to see the dataset ordered by GRUPO2TRUSTNH
dataallcheckpb3 <- dataallcheckpb3 %>% 
  arrange (GRUPO2TRUSTNH)

#to see if it is all ok and see how many we have in each subgroup
dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRUSTNH,WHYTRUSTNH_FOLLOW_Dictado, WHYTRUSTNH_EXP_Grabacion_1, WHYTRUSTNH_EXP_TEXT, WHYTRUSTNH_FOLLOW_DICTAR_RECORDED2, WHYTRUSTNH_FOLLOW_Grabacion_1, WHYTRUSTNH_FOLLOW_TEXT) %>%
  count() %>%
  print(n = 30)

#to see if it is all ok and see how many we have in each subgroup
dataall %>%
  filter(type=="complete" | type=="." | type=="" ) %>% 
  group_by(GRUPO, GRUPO2TRUSTNH) %>%
  count() %>%
  print(n = 30)


##############################################################################
#Save the data 
##############################################################################

write_rds(dataall,file.path(path,"clean_dataallcomplete_addvar.rds"))

dataallnoID = dataall %>%
  select(-id_ea) 

write_rds(dataallnoID,file.path(path,"clean_dataallcomplete_addvarnoID.rds"))


###################################################################################################################
#Create Excel files for coding open questions 
##################################################################################################################

#WHYQUALITY
############

#Select completes 
data = dataall %>% filter(type=="complete")
data = as_factor(data)

datacodingWHYQUALITY <- data %>% 
  select(id_ea, WHYQUALITY_OPEN)

#Export the data subset as an Excel file
#replace"path" by the place where you want to save the file
write_xlsx(datacodingWHYQUALITY, file.path(path,"datacodingWHYQUALITY.xlsx"))


#WHYTRANSP
#############

datacodingWHYTRANSP = data %>%  
  select(id_ea, WHYTRANSP_FINAL)

#replace"path" by the place where you want to save the file
write_xlsx(datacodingWHYTRANSP, file.path(path,"datacodingWHYTRANSP.xlsx"))

#TRUSTNH
#########

datacodingWHYTRUSTNH = data %>%  
  select(id_ea, WHYTRUSTNH_FINAL)

#replace"path" by the place where you want to save the file
write_xlsx(datacodingWHYTRUSTNH, file.path(path,"datacodingWHYTRUSTNH.xlsx"))

#Next steps done in Excel: create one version for each coder, and get them code the different questions

