OpasnetUtils/CheckInput: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{method|moderator=|stub="yes"}} Category:OpasnetUtils ==Description== Checks if an input variable (with the "Inp" -prefix) of the same name as the given argument (ovariabl...") |
(luettavuutta parannettu ja source muutettu muuttujakohtaiseksi) |
||
Line 12: | Line 12: | ||
showcode="1" | showcode="1" | ||
> | > | ||
CheckInput <- function(variable, substitute = FALSE, ...) { | CheckInput <- function(variable, substitute = FALSE, ...) { # e.g for na.rm | ||
if (nrow(variable@output) == 0) stop(paste(variable@name, "output not evaluated yet!")) | if (nrow(variable@output) == 0) stop(paste(variable@name, "output not evaluated yet!")) | ||
if (exists(paste("Inp", variable@name, sep = ""))) { | if (exists(paste("Inp", variable@name, sep = ""))) { | ||
Line 20: | Line 20: | ||
colnames(variable@output)[colnames(variable@output) == "Result"] <- "VarRes" # that would never be used in any data. | colnames(variable@output)[colnames(variable@output) == "Result"] <- "VarRes" # that would never be used in any data. | ||
finalvar <- merge(variable, inputvar) | finalvar <- merge(variable, inputvar) | ||
finalvar@output$Result <- ifelse(is.na(finalvar@output$InpVarRes), finalvar@output$VarRes, finalvar@output$InpVarRes) | finalvar@output$Result <- ifelse( | ||
finalvar@output | is.na(finalvar@output$InpVarRes), | ||
finalvar@output$VarRes, | |||
finalvar@output$InpVarRes | |||
) | |||
finalvar@output[,paste(variable@name, "Source", sep = "_")] <- ifelse( | |||
is.na(finalvar@output$InpVarRes), | |||
finalvar@output[,paste(variable@name, "Source", sep = "_")], | |||
"Input" | |||
) | |||
return(finalvar[!colnames(finalvar) %in% c("InpVarRes", "VarRes")]) | return(finalvar[!colnames(finalvar) %in% c("InpVarRes", "VarRes")]) | ||
} | } | ||
#variable@output[variable@output$Source,] | |||
j <- levels(variable@output[,paste(variable@name, "Source", sep = "_")]) | |||
temp <- merge(temp, variable@output[variable@output | temp <- j[1] | ||
for (i in j[!j == j[1]]) { | |||
temp <- merge( | |||
temp, | |||
variable@output[ | |||
variable@output[,paste(variable@name, "Source", sep = "_")] == i, | |||
!colnames(variable@output) %in% paste(variable@name, "Source", sep = "_") | |||
] | |||
) | |||
} | } | ||
return(melt(temp, measure.vars = levels(variable@output | colnames(temp)[colnames(temp) %in% "Result"] <- i | ||
return( | |||
melt( | |||
temp, | |||
measure.vars = levels(variable@output[,paste(variable@name, "Source", sep = "_")]), | |||
variable.name = paste(variable@name, "Source", sep = "_"), | |||
value.name = "Result", | |||
... | |||
) | |||
) | |||
} | } | ||
return(variable@output) | return(variable@output) |
Revision as of 11:34, 18 June 2012
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
Checks if an input variable (with the "Inp" -prefix) of the same name as the given argument (ovariable) exists. If input exists appends or substitutes it to the output and marks the source as "Input". Returns a data.frame.
Code
CheckInput <- function(variable, substitute = FALSE, ...) { # e.g for na.rm if (nrow(variable@output) == 0) stop(paste(variable@name, "output not evaluated yet!")) if (exists(paste("Inp", variable@name, sep = ""))) { inputvar <- get(paste("Inp", variable@name, sep = "")) if (substitute) { colnames(inputvar@output)[colnames(inputvar@output) == "Result"] <- "InpVarRes" # Should probably be changed to something colnames(variable@output)[colnames(variable@output) == "Result"] <- "VarRes" # that would never be used in any data. finalvar <- merge(variable, inputvar) finalvar@output$Result <- ifelse( is.na(finalvar@output$InpVarRes), finalvar@output$VarRes, finalvar@output$InpVarRes ) finalvar@output[,paste(variable@name, "Source", sep = "_")] <- ifelse( is.na(finalvar@output$InpVarRes), finalvar@output[,paste(variable@name, "Source", sep = "_")], "Input" ) return(finalvar[!colnames(finalvar) %in% c("InpVarRes", "VarRes")]) } #variable@output[variable@output$Source,] j <- levels(variable@output[,paste(variable@name, "Source", sep = "_")]) temp <- j[1] for (i in j[!j == j[1]]) { temp <- merge( temp, variable@output[ variable@output[,paste(variable@name, "Source", sep = "_")] == i, !colnames(variable@output) %in% paste(variable@name, "Source", sep = "_") ] ) } colnames(temp)[colnames(temp) %in% "Result"] <- i return( melt( temp, measure.vars = levels(variable@output[,paste(variable@name, "Source", sep = "_")]), variable.name = paste(variable@name, "Source", sep = "_"), value.name = "Result", ... ) ) } return(variable@output) } |