Created
March 18, 2019 20:29
-
-
Save sean-mcclure/34c87f68c2bfdccdec27799a946c4445 to your computer and use it in GitHub Desktop.
Example Functions for Building an R Library
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
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