Skip to content

Commit

Permalink
Replace functions are modified
Browse files Browse the repository at this point in the history
  • Loading branch information
seokhoonj committed Feb 26, 2024
1 parent cfbc163 commit 6cc89bd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 84 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ LinkingTo:
Rcpp,
RcppArmadillo
Suggests:
testthat (>= 3.0.0)
testthat (>= 3.0.0),
pryr (>= 0.1.6)
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export(min_by_rownames)
export(mkdir)
export(mostfreq)
export(paste_list)
export(replace_empty_with_na)
export(replace_na_with_zero)
export(reverse)
export(rotate)
export(row_max)
Expand Down
55 changes: 0 additions & 55 deletions R/group.R

This file was deleted.

51 changes: 51 additions & 0 deletions R/replace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Replace NA with zero
#'
#' Replace NA_integer_ or NA_real_ values with zero in a memory-efficient way
#'
#' @param df a data frame
#' @return no return value
#'
#' @examples
#' \donttest{df <- data.frame(x = c(1, NA, 3), y = c("A", "B", NA), z = c(NA, 5, NA))
#' pryr::address(df)
#' replace_na_with_zero(df)
#' pryr::address(df)
#' df}
#'
#' @export
replace_na_with_zero <- function(df) {
old_class <- class(df)
set_dt(df)
class <- sapply(df, class)
cols <- names(class)[which(class %in% c("numeric", "integer"))]
df[, `:=`((cols), lapply(.SD, function(x) ifelse(is.na(x), 0, x))),
.SDcols = cols]
setattr(df, "class", old_class)
invisible(df)
}

#' Replace empty with NA
#'
#' Replace empty string like "" with NA_character_ in a memory-efficient way
#'
#' @param df a data frame
#' @return no return value
#'
#' @examples
#' \donttest{df <- data.frame(x = c("A", "B", ""), y = c(1, NA, 3), z = c("", "E", ""))
#' pryr::address(df)
#' replace_empty_with_na(df)
#' pryr::address(df)
#' df}
#'
#' @export
replace_empty_with_na <- function(df) {
old_class <- class(df)
set_dt(df)
class <- sapply(df, class)
cols <- names(class)[which(class == "character")]
df[, `:=`((cols), lapply(.SD, function(x) ifelse(x == "", NA, x))),
.SDcols = cols]
setattr(df, "class", old_class)
invisible(df)
}
28 changes: 0 additions & 28 deletions R/string.R

This file was deleted.

25 changes: 25 additions & 0 deletions man/replace_empty_with_na.Rd

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

25 changes: 25 additions & 0 deletions man/replace_na_with_zero.Rd

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

0 comments on commit 6cc89bd

Please sign in to comment.