overwrite_df: Replace all matching values in a dataframe with something...

View source: R/dataframe_tools.R

overwrite_dfR Documentation

Replace all matching values in a dataframe with something else

Description

Uses regex to match and replace cell values. This function is meant for formatting printed data; it's often useful to blank out NAs or other values to minimise visual clutter when you are reading a table. For actual data-tidying applications, it's safer to use dplyr::recode() or dplyr::recode_factor().

Usage

overwrite_df(
  df,
  find = "^(NA||\\s+|0|-+|_+)$",
  replace = "",
  replace_na = TRUE
)

Arguments

df

(Dataframe) A dataframe.

find

(Character) A regex search pattern.

replace

(Character) The string used to overwrite the matching cells.

replace_na

(Logical) If TRUE, also overwrite R's built-in NA values.

Details

By default, this function will replace cells consisting of NAs, spaces, empty strings, dashes, and underscores with an empty string.

Value

A dataframe.

Authors

Examples

test_df <-
    data.frame(stringsAsFactors = FALSE,
               name = c("insect1", "insect2", "insect3", "insect4", "insect5",
                        "insect6", "insect7", "insect8", "insect9", "insect10"),
               family = c("Belidae", "Belidae", " ", "Coccinelidae", NA, "Coccinelidae",
                          "Braconidae", "_", "-", "Curculionidae"),
               is_cool = c("TRUE", "TRUE", NA, "TRUE", "", "TRUE", "TRUE", "-", "_",
                           "TRUE")
    )

test_df
overwrite_df(test_df)


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.