Last active
August 29, 2015 14:00
-
-
Save hardingnj/ba9f889164602d74edc9 to your computer and use it in GitHub Desktop.
A simple function that takes a dataframe and compares named columns to one another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply.function.colwise <- function(FUNC, x, x.columns = rownames(x), y.columns = colnames(x), ignore.diag = identical(x.columns, y.columns), ...) { | |
checks <- c(is.numeric, is.character); | |
# passing in a factor results in unexpected results, due to implicit numeric recasting | |
stopifnot( | |
any(sapply(checks, function(FUN) FUN(y.columns))), | |
any(sapply(checks, function(FUN) FUN(x.columns))) | |
); | |
distance.matrix <- matrix( | |
NA, | |
nrow = length(y.columns), | |
ncol = length(x.columns), | |
dimnames = list(y.columns, x.columns) | |
); | |
# could be smarter if function is known to be symmetric like most distance metrics, but will | |
# leave as is in case ppl want to use inv() etc | |
for(i in 1:nrow(distance.matrix)){ | |
for(j in 1:ncol(distance.matrix)){ | |
if(ignore.diag & i == j) next; | |
distance.matrix[i, j] <- FUNC( | |
x[,y.columns[i]], | |
x[,x.columns[j]], | |
... | |
); | |
} | |
} | |
return(distance.matrix); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: