Skip to content

Instantly share code, notes, and snippets.

@glamp
Last active August 3, 2016 21:52
Show Gist options
  • Save glamp/052d6c0e24c7cb1f71c334e09b16dd6d to your computer and use it in GitHub Desktop.
Save glamp/052d6c0e24c7cb1f71c334e09b16dd6d to your computer and use it in GitHub Desktop.
dtypes <- list(
"foo"=as.character,
"bar"=as.integer,
"letters"=function(x) {
factor(x, levels=c("a", "b", "c"))
}
)
ensureAllVariablesExist <- function(df) {
has.types <- names(dtypes) %in% names(df)
sum(has.types)==length(dtypes)
}
ensureTypes <- function(df) {
for(dtype in names(dtypes)) {
df[,dtype] <- dtypes[[dtype]](df[,dtype])
}
df
}
column_order <- c("d", "b", "c", "a")
ensureOrder <- function(df) {
df[,column_order]
}
model.predict <- function(df) {
if (! ensureAllVariablesExist(df)) {
missing.vars <- names(dtypes)[! names(dtypes) %in% names(df)]
return (list(error="missing variables", "missingVariables"=missing.vars))
}
df <- ensureTypes(df)
df <- ensureOrder(df)
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment