-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc3b4ca
commit 20aa493
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: messy | ||
Title: Create messy data from clean dataframes | ||
Version: 0.0.1 | ||
Version: 0.0.1001 | ||
Authors@R: c( | ||
person(given = "Nicola", family = "Rennie", role = c("aut", "cre", "cph"), | ||
email = "[email protected]", comment = c(ORCID = "0000-0003-4797-557X"))) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Make character vector messy | ||
#' | ||
#' Adds special characters and randomly | ||
#' capitalises characters in the provided | ||
#' character vector. | ||
#' | ||
#' @paramd x string vector to mess up | ||
#' @return string vector that is messed up | ||
#' @examples | ||
#' | ||
#' make_string_messy(c("Hello", "world")) | ||
make_string_messy <- function(x){ | ||
sapply(x, messy_string, USE.NAMES = FALSE) | ||
} | ||
|
||
#' Make column names messy | ||
#' | ||
#' Adds special characters and randomly | ||
#' capitalises characters in the column | ||
#' names of a data frame. | ||
#' @paramd df data.frame to alter column names | ||
#' @return data.frame with messy column names | ||
#' @examples | ||
#' | ||
#' make_column_names_messy(iris) | ||
make_column_names_messy <- function(df) { | ||
# Assign the new column names to the dataframe | ||
names(df) <- make_string_messy(names(df)) | ||
|
||
return(df) | ||
} | ||
|
||
#' Function to make a string messy | ||
#' | ||
#' Adds special characters and randomly | ||
#' capitalises strings. | ||
#' @params s string to mess up | ||
#' @return messy string | ||
#' @noRd | ||
messy_string <- function(s) { | ||
random_chars <- c("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", ".") | ||
|
||
# Convert to vector of characters | ||
chars <- strsplit(s, NULL)[[1]] | ||
|
||
# Randomly change the case of each character using sapply | ||
chars <- sapply(chars, function(char) { | ||
if (runif(1) < 0.5) { | ||
return(toupper(char)) | ||
} else { | ||
return(tolower(char)) | ||
} | ||
}) | ||
|
||
# Randomly insert special characters using lapply | ||
chars <- Reduce(function(acc, char) { | ||
if (runif(1) < 0.2) { | ||
char_to_insert <- sample(random_chars, 1) | ||
return(c(acc, char_to_insert, char)) | ||
} else { | ||
return(c(acc, char)) | ||
} | ||
}, chars, init = character(0)) | ||
|
||
# Reassemble the string | ||
return(paste(chars, collapse = "")) | ||
} |