Changing column types with dplyr

后端 未结 3 1182
南旧
南旧 2021-02-11 03:28

I need some help tidying my data. I\'m trying to convert some integers to factors (but not all integers to factors). I think I can do with selecting the variables in question

3条回答
  •  一整个雨季
    2021-02-11 04:12

    As of dplyr 1.0.0 released on CRAN 2020-06-01, the scoped functions mutate_at(), mutate_if() and mutate_all() have been superseded thanks to the more generalizable across(). This means you can stay with just mutate(). The introductory blog post from April explains why it took so long to discover.

    Toy example:

    library(dplyr)
    
    iris %>%
      mutate(across(c(Sepal.Width, 
                      Sepal.Length),
                    factor))
    

    In your case, you'd do this:

    library(dplyr)
    
    raw_data_tbl %>% 
      mutate(across(c(is.numeric,
                      -contains("units"),
                      -c(PRO_ALLOW, RTL_ACTUAL, REAL_PRICE, REAL_PRICE_HHU,
                         REBATE, RETURN_UNITS, UNITS_PER_CASE, Profit,
                         STR_COST, DCC, CREDIT_AMT)),
                    factor))
    

提交回复
热议问题