forked from mrdwab/koboloadeR
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkobo_encode.R
228 lines (193 loc) · 10.2 KB
/
kobo_encode.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#' @name kobo_encode
#' @rdname kobo_encode
#' @title Encode variable
#'
#' @description Insert the full label in data frame based on dictionnary
#'
#'
#' @param data Dataframe to relabel
#' @param dico Data dictionnary generated from kobo_dico
#'
#'
#' @return A "data.table" with the full data.label. To be used for graphs generation.
#'
#' @author Edouard Legoupil
#'
#'
#' @export kobo_encode
#'
#' @examples
#' \dontrun{
#' kobo_encode(data, dico)
#' }
#'
kobo_encode <- function(data, dico) {
### First we provide attribute label to variable name
#data1 <- data
# MainDataFrame <- read.csv("data/MainDataFrame_edited.csv")
# dico <- read.csv("data/dico_form.xls.csv", comment.char="#")
# data <- MainDataFrame
data.label <- as.data.frame(names(data))
names(data.label)[1] <- "fullname"
data.label <- plyr::join(x = data.label, y = dico, by = "fullname", type = "left" )
## Now we can also re-encode the records themself
###### Case 1: Re-encoding when we have select_multiple ####
## List of select one and select multiple variable to re-encode ## "select_one",
selectdf <- as.data.frame(dico[dico$type %in% c( "select_multiple"), c("fullname","name","listname","type")])
### Verify that those variable are actually in the original dataframe
check <- as.data.frame(names(data))
names(check)[1] <- "fullname"
check$id <- row.names(check)
selectdf2 <- plyr::join(y = check, x = selectdf, by = "fullname", type = "left")
selectdf3 <- selectdf[!is.na(selectdf2$id), ]
if (nrow(selectdf3) == 0) {
cat("************************************************************\n \n")
cat("There's no disagreggated select_multiple variables to re-encoded \n")
cat("No match for Select multiple variable in your dataset! \n")
cat(" You may double check that the form and the data are matching \n")
cat("Double check as well that you did download the data with the correct header (i.e. full path with point delimiters) \n")
cat("************************************************************\n \n")
} else{
#names(selectdf)[1] <- "selectvar"
cat(paste0("There's ",nrow(selectdf3)," select_multiple modalities to encode \n"))
for (i in 1:nrow(selectdf3)) {
# i <- 98
fullname <- as.character(selectdf3[ i,1])
variablename <- as.character(selectdf3[ i,2])
variablelistname <- as.character(selectdf3[ i,3])
# variablelevel <- as.data.frame(levels(as.factor(data[ ,fullname])))
variablelevel <- as.data.frame(levels(as.factor(data[[fullname]])))
names(variablelevel)[1] <- "namecoded"
labelchoice <- as.character(dico[dico$fullname == fullname, c("labelchoice")])
if (nrow(variablelevel) > 0) {
if (nrow(variablelevel) > 1) {
data[ , fullname][is.na(data[ , fullname])] <- "Not replied"
data[ , fullname][data[ , fullname] == "Not replied"] <- "Not replied"
data[ , fullname][data[ , fullname] == "0"] <- "Not selected"
data[ , fullname][data[ , fullname] == "FALSE"] <- "Not selected"
data[ , fullname][data[ , fullname] == "False"] <- "Not selected"
data[ , fullname][data[ , fullname] == "1"] <- labelchoice
data[ , fullname][data[ , fullname] == "TRUE"] <- labelchoice
data[ , fullname][data[ , fullname] == "True"] <- labelchoice
} else{
data[ , fullname][is.na(data[ , fullname])] <- ""
data[ , fullname][data[ , fullname] == "0"] <- ""
data[ , fullname][data[ , fullname] == "FALSE"] <- ""
data[ , fullname][data[ , fullname] == "False"] <- ""
data[ , fullname][data[ , fullname] == "1"] <- labelchoice
data[ , fullname][data[ , fullname] == "TRUE"] <- labelchoice
data[ , fullname][data[ , fullname] == "True"] <- labelchoice
}
cat(paste0(i, "- Recode disagreggated select_multiple modality ", fullname," for: ",labelchoice, "\n"))
} else{cat(paste0("The following variable has no answers to recode in the dataset: ",fullname, "\n")) }
rm(fullname, variablename, variablelistname,variablelevel)
}
}
###### Case 2: Re-encoding when we have disaggregated select_one - select_one_d ####
## List of select one and select multiple variable to re-encode ## "select_one",
selectdf <- as.data.frame(dico[dico$type %in% c( "select_one_d"), c("fullname","name","listname","type")])
### Verify that those variable are actually in the original dataframe
check <- as.data.frame(names(data))
names(check)[1] <- "fullname"
check$id <- row.names(check)
selectdf2 <- plyr::join(y = check, x = selectdf, by = "fullname", type = "left")
selectdf3 <- selectdf[!is.na(selectdf2$id), ]
if (nrow(selectdf3) == 0) {
cat("************************************************************\n \n")
cat("There's no disaggregated select_one variable to re-encoded \n")
cat("i.e. No match for Select one variables in your dataset! \n")
cat(" You may double check that the form and the data are matching \n")
cat("Double check as well that you did download the data with the correct header (i.e. full path with point delimiters) \n")
cat("************************************************************\n \n")
} else{
#names(selectdf)[1] <- "selectvar"
for (j in 1:nrow(selectdf3)) {
#j <-228
#j <-1
#i <-195
#cat(i)
fullname <- as.character(selectdf3[ j,1])
variablename <- as.character(selectdf3[ j,2])
variablelistname <- as.character(selectdf3[ j,3])
variablelevel <- as.data.frame(levels(as.factor(data[ ,fullname])))
names(variablelevel)[1] <- "namecoded"
variablecode <- as.character(levels(as.factor(variablelevel$namecoded)))
if (nrow(variablelevel) > 0) {
variablelevel <- cbind(variablelevel,fullname,variablename,variablelistname)
variablelevel <- plyr::join(x = variablelevel, y = dico, by = "fullname", type = "left" )
labelchoice <- as.character(dico[dico$fullname == fullname, c("labelchoice")])
data[ , fullname][data[ , fullname] == variablecode] <- labelchoice
cat(paste0(" - Recode disaggregated select_one variable: ", fullname," for: ",labelchoice, "\n"))
#View(data[i])
} else {cat(paste0("The following disaggregated select_one variable has no answers to recode in the dataset: ",fullname, "\n")) }
rm(fullname, variablename, variablelistname,variablelevel)
}
}
###### Case 3: Re-encoding when we have select_select_one ####
## List of select one and select multiple variable to re-encode ## "select_one",
selectdf <- as.data.frame(dico[dico$type %in% c( "select_one"), c("fullname","name","listname","type")])
### Verify that those variable are actually in the original dataframe
check <- as.data.frame(names(data))
names(check)[1] <- "fullname"
check$id <- row.names(check)
selectdf2 <- plyr::join(y = check, x = selectdf, by = "fullname", type = "left")
selectdf3 <- selectdf[!is.na(selectdf2$id), ]
#names(selectdf)[1] <- "selectvar"
if (nrow(selectdf3) == 0) {
cat("************************************************************\n \n")
cat("There's no select_one variables to re-encode \n")
cat("i.e. No match for Select one variables in your dataset! \n")
cat(" You may double check that the form and the data are matching \n")
cat("Double check as well that you did download the data with the correct header (i.e. full path with point delimiters) \n")
cat("************************************************************\n \n")
} else {
cat(paste0("There's ",nrow(selectdf3)," select_one variables to encode \n"))
for (i in 1:nrow(selectdf3)) {
# i <- 50
fullname <- as.character(selectdf3[ i,1])
variablename <- as.character(selectdf3[ i,2])
variablelistname <- as.character(selectdf3[ i,3])
variablelevel <- dico[ dico$listname == variablelistname & dico$type == "select_one_d", c("name","labelchoice")]
# Need to account for the case where allow_choice_duplicates = yes
variablelevel <- unique(variablelevel[ c("name","labelchoice")])
if (nrow(variablelevel) > 0) {
#rm(df)
df <- as.data.frame(data[ , fullname])
names(df)[1] <- "name"
df$name <- as.character(df$name)
df <- plyr::join(x = df, y = variablelevel, by = "name")
data[ , fullname] <- as.character(data[ , fullname])
data[ , fullname] <- df$labelchoice
#data[ , fullname] <- as.factor(data[ , fullname])
data[ , fullname] <- as.factor(data[[fullname]])
#View(data[i])
cat(paste0(i, "- Recode variable: ", fullname," \n"))
} else {cat(paste0("The following variable has no answers to recode in the dataset: ",fullname, "\n")) }
rm(fullname, variablename, variablelistname,variablelevel)
}
}
###### Case 4: Re-encoding when we have select_select_multiple ####
## List of select one and select multiple variable to re-encode ## "select_one",
selectdf <- as.data.frame(dico[dico$type %in% c( "select_multiple_d"), c("fullname","name","listname","type")])
### Verify that those variable are actually in the original dataframe
check <- as.data.frame(names(data))
names(check)[1] <- "fullname"
check$id <- row.names(check)
selectdf2 <- plyr::join(y = check, x = selectdf, by = "fullname", type = "left")
selectdf3 <- selectdf[!is.na(selectdf2$id), ]
#names(selectdf)[1] <- "selectvar"
if (nrow(selectdf3) >= 1) {
cat("************************************************************\n \n")
cat("Uhmm... you have concatenated select_multiple. If this not already done, you may need to use the kobo_split_multiple function to deconcatenate those lists. \n")
cat("************************************************************\n \n")
} else{
cat("************************************************************\n \n")
cat("No concatenated select_multiple.! \n")
cat("i.e. No match for Select multiple variables in your dataset! \n")
cat(" You may double check that the form and the data are matching \n")
cat("Double check as well that you did download the data with the correct header (i.e. full path with point delimiters) \n")
cat("************************************************************\n \n")
}
return(data)
}
NULL