Skip to content

Commit

Permalink
Merge pull request #1 from drmowinckels/main
Browse files Browse the repository at this point in the history
add messy string functions
  • Loading branch information
nrennie authored Nov 17, 2024
2 parents bc3b4ca + db63665 commit bec13eb
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
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")))
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export(add_whitespace)
export(change_case)
export(make_column_names_messy)
export(make_missing)
export(make_string_messy)
export(messy)
importFrom(rlang,.data)
importFrom(stats,runif)
68 changes: 68 additions & 0 deletions R/messy_strings.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#' Make character vector messy
#'

Check warning on line 2 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=2,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' Adds special characters and randomly
#' capitalises characters in the provided
#' character vector.
#'

Check warning on line 6 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=6,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @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

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=12,col=33,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 12 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=12,col=33,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
sapply(x, messy_string, USE.NAMES = FALSE)
}

#' Make column names messy
#'

Check warning on line 17 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=17,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' 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))

Check warning on line 29 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=29,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
return(df)
}

#' Function to make a string messy
#'

Check warning on line 34 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=34,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' 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("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", ".")

Check warning on line 42 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=42,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 89 characters.

# Convert to vector of characters
chars <- strsplit(s, NULL)[[1]]

Check warning on line 46 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=46,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# 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))
}
})

Check warning on line 55 in R/messy_strings.R

View workflow job for this annotation

GitHub Actions / lint

file=R/messy_strings.R,line=55,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# 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 = ""))
}
22 changes: 22 additions & 0 deletions man/make_column_names_messy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/make_string_messy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bec13eb

Please sign in to comment.