Difference between revisions of "OpasnetUtils/Interpret"

From Testiwiki
Jump to: navigation, search
m
m
Line 14: Line 14:
 
label="Initiate functions"
 
label="Initiate functions"
 
graphics="1"
 
graphics="1"
 +
showcode="1"
 
>
 
>
 
# INTERPRET ################### interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).
 
# INTERPRET ################### interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).

Revision as of 10:49, 15 June 2012



Description

interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).

It also changes abbreviations into probability samples.

Code

- Hide code

# INTERPRET ################### interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).
### It also changes abbreviations into probability samples.
interpret <- function(data) {
	sample <- NULL
	if(is.vector(data)) {data <- data.frame(Result = data)}
	if("Iter" %in% colnames(data)) {
		out <- data}
	else {
		if(!"Result" %in% colnames(data)) {cat("There MUST be an observation column named 'Result'.\n")}
		test <- !is.na(as.numeric(as.character(data$Result)))
	
		for(i in 1:nrow(data)) {
			if(test[i]) {
				sample <- c(sample, rep(as.numeric(as.character(data[i, "Result"])), n))
			} else {
				samplingguide <- as.numeric(strsplit(gsub(" ", "", data[i, "Result"]), "-")[[1]])
				if(is.na(samplingguide[1]) | is.na(samplingguide[2])) {
					sample <- c(sample, rep(data[i, "Result"], n))
				} else {
					sample <- c(sample, runif(n, samplingguide[1], samplingguide[2]))
				}
			}
		}
	
		out <- as.data.frame(array(1:(n*nrow(data)*(ncol(data)+1)), dim = c(n*nrow(data), ncol(data) + 1)))
		colnames(out) <- c("Iter", colnames(data))
	
		for(i in colnames(data)) {
			out[i] <- rep(data[, i], each = n)
		}
		out$Iter <- 1:n
		out$Result <- sample
		
	}
	return(out)
}

See also