corrupt.factory: corrupt.factory

Description Usage Arguments Value Author(s) Examples

Description

Given a function that performs a "corruption" on data, corrupt.factory will generate a function that takes some data x, and with probability error_rate performs the given corruption on it.

Usage

1
2
corrupt.factory(corr.func, type_check = function(x) {     TRUE },
  error_message = NULL)

Arguments

corr.func

The corruption function. Must take at least one value

type_check

A function that checks the type of the value given to the corruption function. Default behavior allows any type to be passed to the function. Must take at least one argument, and return a boolean.

error_message

A error message to print if type_check fails. Optional, where default behavior throws a general error message.

Value

result(val, error_rate = .2, ...), a function that can take a value and an error rate, (Along with other parameters), returns either the same value(val) passed in, or that value subjected to corr.func(corr.func(val)) wiith probability = error_rate.

Author(s)

Sam Murray<slmurray@andrew.cmu.edu>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Say I have a corruption function reverse, that takes a string and returns the reverse of that string

string_rev <- function(s){
   split_str = unlist(strsplit(s))
   rev_str = rev(split_str)
   return(paste0(rev_str))
}

#Now we use corrupt.factory to make a function that only returns the reverse of the function with probability = error_rate
#Note: We only want to have this work on strings, so we pass in is.character as type_check, and a helpful error mesage
string_rev_corr <- corrupt.factory(string_rev, type_check = is.character, error_message = "string_rev_corr cannot reverse a non-character value")

#Now we can use the result to corrupt a string of values. The resulting vector should have around 30 of the "hello"s replaced with "olleh".
str_vec <- rep("hello", times = 100)
corr_string_vec = sapply(str_vec, string_rev_corr, error_rate = .3)

Sam-Murray/RecordLinkUtil documentation built on Oct. 30, 2019, 11:48 p.m.