Skip to content

Instantly share code, notes, and snippets.

@sean-mcclure
Created March 18, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sean-mcclure/34c87f68c2bfdccdec27799a946c4445 to your computer and use it in GitHub Desktop.
Save sean-mcclure/34c87f68c2bfdccdec27799a946c4445 to your computer and use it in GitHub Desktop.
Example Functions for Building an R Library
library(data.table)
library(mltools)
encode_and_bind <- function(frame, feature_to_encode) {
res <- cbind(iris, one_hot(as.data.table(frame[[feature_to_encode]])))
return(res)
}
remove_features <- function(frame, features) {
rem_vec <- unlist(strsplit(features, ', '))
res <- frame[,!(names(frame) %in% rem_vec)]
return(res)
}
apply_function_to_column <- function(frame, list_of_columns, new_col, funct) {
use_cols <- unlist(strsplit(list_of_columns, ', '))
new_cols <- unlist(strsplit(new_col, ', '))
frame[new_cols] <- apply(frame[use_cols], 2, function(x) {eval(parse(text=funct))})
return(frame)
}
get_closest_string <- function(vector_of_strings, search_string) {
all_dists <- adist(vector_of_strings, search_string)
closest <- min(all_dists)
res <- vector_of_strings[which(all_dists == closest)]
return(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment