OpasnetUtils/Tidy: Difference between revisions

From Opasnet
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 15: Line 15:
label="Initiate functions"
label="Initiate functions"
graphics="1"
graphics="1"
showcode="1"
>
>
# TIDY ########### tidy: a function that cleans the tables from Opasnet Base
# TIDY ########### tidy: a function that cleans the tables from Opasnet Base

Revision as of 10:52, 15 June 2012



Description

a function that cleans the tables from Opasnet Base

Parameters

  • data is a table from op_baseGetData function

Code

- Hide 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)
}

See also