EVOLUTION-MANAGER
Edit File: fct_relabel.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Automatically relabel factor levels, collapse as necessary</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for fct_relabel {forcats}"><tr><td>fct_relabel {forcats}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Automatically relabel factor levels, collapse as necessary</h2> <h3>Description</h3> <p>Automatically relabel factor levels, collapse as necessary </p> <h3>Usage</h3> <pre> fct_relabel(.f, .fun, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.f</code></td> <td> <p>A factor (or character vector).</p> </td></tr> <tr valign="top"><td><code>.fun</code></td> <td> <p>A function to be applied to each level. Must accept one character argument and return a character vector of the same length as its input. </p> <p>You can also use <code>~</code> to create as shorthand (in the style of purrr). <code>~ paste(., "x")</code> is equivalent to <code>function(.) paste(., "x")</code></p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments to <code>fun</code>.</p> </td></tr> </table> <h3>Examples</h3> <pre> gss_cat$partyid %>% fct_count() gss_cat$partyid %>% fct_relabel(~ gsub(",", ", ", .x)) %>% fct_count() convert_income <- function(x) { regex <- "^(?:Lt |)[$]([0-9]+).*$" is_range <- grepl(regex, x) num_income <- as.numeric(gsub(regex, "\\1", x[is_range])) num_income <- trunc(num_income / 5000) * 5000 x[is_range] <- paste0("Gt $", num_income) x } fct_count(gss_cat$rincome) convert_income(levels(gss_cat$rincome)) rincome2 <- fct_relabel(gss_cat$rincome, convert_income) fct_count(rincome2) </pre> <hr /><div style="text-align: center;">[Package <em>forcats</em> version 0.5.0 <a href="00Index.html">Index</a>]</div> </body></html>