Skip to content

Instantly share code, notes, and snippets.

@hardingnj
Last active August 29, 2015 14:00
Show Gist options
  • Save hardingnj/ba9f889164602d74edc9 to your computer and use it in GitHub Desktop.
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
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);
}
@hardingnj
Copy link
Author

Usage:

library(datasets)
apply.function.colwise(
  cor, 
  x = Seatbelts, 
  x.columns = colnames(Seatbelts), 
  y.columns = colnames(Seatbelts),
  method = sample(c("pearson", "spearman"),1) 
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment