forked from mrdwab/koboloadeR
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkobo_consolidateone.R
77 lines (66 loc) · 2.47 KB
/
kobo_consolidateone.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
#' @name kobo_consolidateone
#' @rdname kobo_consolidateone
#' @title Merge disagregated select_one variable
#'
#' @description Merge disagregated select_one variable
#'
#' @param frame kobo or odk exported dataset to use
#' @param form The full filename of the form to be accessed (has to be an xls file).
#'
#' @author Edouard Legoupil
#'
#'
#' @export kobo_consolidateone
#'
#' @examples
#' \dontrun{
#' kobo_consolidateone(frame , form = "form.xlsx")
#' }
#'
#'
#' @return data
kobo_consolidateone <- function(frame , form = "form.xlsx") {
configInfo <- kobo_get_config()
mainDir <- kobo_getMainDirectory()
dico <- utils::read.csv(paste0(mainDir,"/data/dico_",form,".csv"), encoding = "UTF-8", na.strings = "")
### List of select_one questions
listoned <- dico[dico$type == "select_one_d" , c("listname","label","name","fullname")]
listoned$qname <- ""
for (i in 1:nrow(listoned)) {
listoned[i,5] <- substr(as.character(listoned[i,4]), 1 , nchar(as.character(listoned[i,4])) - 1 - nchar(as.character(listoned[i,3])))
}
check <- as.data.frame(names(data))
names(check)[1] <- "fullname"
check$id <- row.names(check)
listoned2 <- plyr::join(x = listoned, y = check, by = "fullname", type = "left")
listoned <- listoned2[!is.na(listoned2$id), ]
## List of sub select_one
liston <- dico[dico$type == "select_one" & is.na(dico$qrepeat), c("listname","label","name","fullname")]
# liston2 <- plyr::join(x=liston, y=check, by="fullname", type="left")
# liston3 <- liston2[!is.na(liston$id), ]
names(liston)[4] <- "qname"
# test <- plyr::join(y=liston, x=listoned, type="left", by="qname")
for (i in 1:nrow(liston) ) {
# i <- 1
newlistone <- as.character(liston[i,4])
### select variable for a specific multiple questions
selectlistoned <- as.data.frame(listoned[listoned$qname == newlistone , c("fullname")])
names(selectlistoned)[1] <- "vartoconact"
selectlistoned$vartoconact <- as.character(selectlistoned$vartoconact)
#data <- data[ , c(1:745)]
data$newvar <- ""
for (j in 1:nrow(selectlistoned) ) {
#j <- 7
if (!is.na(selectlistoned[j , 1]) ) {
data[ ,selectlistoned[j,1] ][is.na(data[ ,selectlistoned[j,1] ])] <- ""
data$newvar <- paste0(data$newvar, data[ ,selectlistoned[j,1] ] )
cat(paste("i=",i," - j=",j,"\n"))
} else { }
}
#View(data$newvar)
names(data)[names(data) == "newvar"] <- newlistone
}
#rm(liston,listoned,selectlistoned,i,newlistone)
return(data)
}
NULL