OpasnetUtils/Tidy
Moderator:Nobody (see all) Click here to sign up. |
This page is a stub. You may improve it into a full page. |
Upload data
|
Description
a function that cleans the tables from Opasnet Base
Parameters
- data is a table from op_baseGetData function
Code
# TIDY ########### tidy: a function that cleans the tables from Opasnet Base # data is a table from op_baseGetData function tidy <- function (data, idvar = "obs", direction = "wide") { data$Result <- ifelse(!is.na(data$Result.Text), as.character(data$Result.Text), data$Result) if("Observation" %in% colnames(data)){test <- data$Observation != "Description"} else {test <- TRUE} data <- data[test, !colnames(data) %in% c("id", "Result.Text")] if("obs.1" %in% colnames(data)) {data[, "obs"] <- data[, "obs.1"]} # this line is temporarily needed until the obs.1 bug is fixed. data <- data[colnames(data) != "obs.1"] if("Row" %in% colnames(data)) { # If user has given Row, it is used instead of automatic obs. data <- data[, colnames(data) != "obs"] colnames(data)[colnames(data) == "Row"] <- "obs" } if(direction == "wide" & "Observation" %in% colnames(data)) { data <- reshape(data, idvar = idvar, timevar = "Observation", v.names = "Result", direction = "wide") data <- data[colnames(data) != "obs"] colnames(data) <- gsub("^Result.", "", colnames(data)) colnames(data)[colnames(data) == "result"] <- "Result" colnames(data)[colnames(data) == "Amount"] <- "Result" } else { data <- data[colnames(data) != "obs"] } return(data) } |