-
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.
Merge pull request #1 from drmowinckels/main
add messy string functions
- Loading branch information
Showing
5 changed files
with
116 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
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,68 @@ | ||
#' Make character vector messy | ||
#' | ||
#' Adds special characters and randomly | ||
#' capitalises characters in the provided | ||
#' character vector. | ||
#' | ||
#' @param x string vector to mess up | ||
#' @return string vector that is messed up | ||
#' @export | ||
#' @examples | ||
#' make_string_messy(c("Hello", "world")) | ||
make_string_messy <- function(x){ | ||
Check warning on line 12 in R/messy_strings.R GitHub Actions / lint
|
||
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. | ||
#' @param df data.frame to alter column names | ||
#' @return data.frame with messy column names | ||
#' @export | ||
#' @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. | ||
#' @param s string to mess up | ||
#' @return messy string | ||
#' @importFrom stats runif | ||
#' @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 = "")) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.