Purrr map con rename_with

Sep 04 2020

Sto cercando di pulire i nomi di un set di dati. Ho usato janitor::clean_names()per iniziare. Tuttavia, ho ancora delle abbreviazioni che vorrei separare con un trattino basso _. Ho un codice che funziona usando rename_with(~str_replace(.x, "gh", "gh_"), .cols = starts_with("gh")), tuttavia ci sono molte abbreviazioni e sarebbe bene trovare un modo mapo funzionalizzare in altro modo questo processo.

dat <- tibble(ghrisk_value = c(1,2), 
              ghrisk_corrected = c(2,3), 
              devpolicy_value = c(4,5),
              devpolicy_corrected = c(5,6))

# code works but not functionalized
dat %>%
   rename_with(~str_replace(.x, "gh", "gh_"), .cols = starts_with("gh")) %>%
   rename_with(~str_replace(.x, "dev", "dev_"), .cols = starts_with("dev")) %>%
   names()

# attempt at map...
abbr_words <- c("gh", "dev")
map(dat, ~rename_with(str_replace(.x, abbr_words, str_c(abbr_words, "_"))) 

Risposte

3 DarrenTsai Sep 04 2020 at 01:49

Non hai bisogno map(). Usa semplicemente la sintassi delle espressioni regolari "(?<=a|b|c)", che corrisponde alla posizione dietro ao bo ce inserisci un trattino basso. Inoltre, starts_with()può prendere un vettore di caratteri come input per abbinare l'unione di tutti gli elementi.

abbr_words <- c("gh", "dev")

pattern <- sprintf("(?<=%s)", str_c(abbr_words, collapse = "|"))
# [1] "(?<=gh|dev)"

dat %>%
  rename_with(~ str_replace(.x, pattern, "_"), starts_with(abbr_words))

# # A tibble: 2 x 4
#   gh_risk_value gh_risk_corrected dev_policy_value dev_policy_corrected
#           <dbl>             <dbl>            <dbl>                <dbl>
# 1             1                 2                4                    5
# 2             2                 3                5                    6
2 IceCreamToucan Sep 04 2020 at 02:25

Puoi reducesostituire le parole constr_replace

abbr_words <- c("gh", "dev")

dat %>% 
  rename_all( ~
    reduce(abbr_words, ~str_replace(.x, paste0('^', .y), paste0(.y, '_')), .init = names(dat))
  )

# # A tibble: 2 x 4
#   gh_risk_value gh_risk_corrected dev_policy_value dev_policy_corrected
#           <dbl>             <dbl>            <dbl>                <dbl>
# 1             1                 2                4                    5
# 2             2                 3                5                    6
2 siddharthapachhai Sep 04 2020 at 02:29

Usando map avrai bisogno di una funzione di assistenza, che è real_func. map funzionerà su colnames (dat) e funzionerà con un colname alla volta. La mappa richiede una funzione che è real_func, il primo parametro, che è il parametro dei dati, andrà prima della funzione e il parametro rimanente andrà dopo. Repl_func prenderà il nome della colonna uno alla volta e prenderà l'elenco delle parole abbreviate, lo sovrapporrà ed eseguirà le sostituzioni. Alla fine unlist, è necessario restituire un vettore appiattito.

abbr_words <- c("gh", "dev")

repl_func <- function(x,y){
  for (i in y){
    x <- str_replace(x,i,paste0(i,"_"))
  }
  return (x)
}


colnames(dat) <- unlist(map(colnames(dat), repl_func, abbr_words))