|
|
(3 intermediate revisions by one other user not shown) |
Line 11: |
Line 11: |
| | | |
| ==Code== | | ==Code== |
− | {{comment|# |Tidy should widen the "Parameter" index as well as "Observation". Also it should enable marginal recognition of the widened variables.|--[[User:Teemu R|Teemu R]] 15:57, 18 June 2012 (EEST)}}
| |
− | :{{comment|# |Recognition could be achieved by adding a "variable name" -prefix to every location under the involved indices.|--[[User:Teemu R|Teemu R]] 15:57, 18 June 2012 (EEST)}}
| |
− | <rcode
| |
− | name="Tidy"
| |
− | label="Initiate functions"
| |
− | graphics="1"
| |
− | showcode="1"
| |
− | >
| |
− | # 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)
| + | https://www.opasnet.org/svn/opasnet_utils/trunk/R/Tidy.r |
− | 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)
| |
− | }
| |
− | </rcode>
| |
| | | |
| ==See also== | | ==See also== |