简体   繁体   English

如何使用dplyr将变量从numeric转换为具有唯一级别的factor

[英]How to use dplyr to convert variables from numeric to factor with unique levels

I am trying to convert a large number of numeric variables into factor variables using a 'codebook' of factor levels (formatted as a list of named lists). 我试图使用因子级别的“代码簿”(格式化为命名列表的列表)将大量数字变量转换为因子变量。 I can do this one by one using mutate() and recode_factor() , but would like to do them all in one go using mutate_at() . 我可以使用mutate()recode_factor()一个接一个地做这个,但是想使用mutate_at()一次性完成它们。 How might I go about this? 我怎么能这样做?

codebook <- list(
  vs = list(`0` = 'V-shaped',
            `1` = 'straight'),
  am = list(`0` = 'automatic',
            `1` = 'manual')
)

mtcars %>%
  mutate(vs = recode_factor(vs, levels = !!!(pluck(codebook, 'vs'))))

mtcars %>%
  mutate_at(vars(names(codebook)),
            funs(recode_factor(., levels = !!!(pluck(codebook, 'somehow_pass_column_name_here?')))))

One option would be to loop through the names of the 'codebook' 一种选择是循环遍历“码本”的names

library(tidyverse)
names(codebook) %>%
   map(~ mtcars %>% 
           transmute(!! .x := recode_factor(!! rlang::sym(.x), 
                        levels  = !!!(pluck(codebook, .x))))) %>% 
   bind_cols(mtcars %>%
   select(-one_of(names(codebook))), .)

or use a for loop 或使用for循环

library(magrittr)
for(nm in names(codebook)) {
  mtcars %<>%
        mutate(!! nm := recode_factor(!! rlang::sym(nm), 
               levels = !!!(pluck(codebook, nm))))
}

You could still use mutate for multiple variables, unless that's what you meant by one-by-one. 您仍然可以将mutate用于多个变量,除非这是您一个接一个的意思。 I'm not well versed on mutate_at , so maybe someone knows that method. 我对mutate_at并不精通,所以也许有人知道这种方法。

mtcars %>%
  mutate(vs = recode_factor(vs, levels = !!!(pluck(codebook, 'vs'))),
         am = recode_factor(am, levels = !!!(pluck(codebook, 'am'))))

    mpg cyl  disp  hp drat    wt  qsec       vs        am gear carb
1  21.0   6 160.0 110 3.90 2.620 16.46 V-shaped    manual    4    4
2  21.0   6 160.0 110 3.90 2.875 17.02 V-shaped    manual    4    4
3  22.8   4 108.0  93 3.85 2.320 18.61 straight    manual    4    1
4  21.4   6 258.0 110 3.08 3.215 19.44 straight automatic    3    1
5  18.7   8 360.0 175 3.15 3.440 17.02 V-shaped automatic    3    2
6  18.1   6 225.0 105 2.76 3.460 20.22 straight automatic    3    1
7  14.3   8 360.0 245 3.21 3.570 15.84 V-shaped automatic    3    4
8  24.4   4 146.7  62 3.69 3.190 20.00 straight automatic    4    2
9  22.8   4 140.8  95 3.92 3.150 22.90 straight automatic    4    2
10 19.2   6 167.6 123 3.92 3.440 18.30 straight automatic    4    4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 dplyr 中的 mutate_each 将所有数值变量转换为因子 - using mutate_each from dplyr to convert all numeric variables to factor 使用 dplyr 在长格式数据上计算因子水平和数值的唯一出现次数 - Count unique occurrences of factor levels and numeric values with dplyr, on data in a long format 以与从 0 到唯一值长度的因子相同的顺序将因子转换为数字 - Convert factor to numeric in the same order of the factor from 0 to length of the unique values 如何将因子有序变量转换为数值 - How to convert factor ordered variables to numeric 使用变量和级别列表将字符数据转换为使用 dplyr 的因子 - Use a list of variables and levels to convert character data to factors using dplyr 如何使用mutate和ifelse将数值变量转换为具有多个级别的因子变量 - How to use mutate and ifelse to convert numerical variables into factor variable with multiple levels r - 将因子转换为数字并删除级别 - r - convert factor to numeric and remove levels 管道,将数值转换为保留未观察到的水平的因子 - Pipes, convert numeric to factor with unobserved levels retained 如何将因子水平转换为 R 中的变量? - How do I convert factor levels to variables in R? 如何将因子转换为数值? - How to convert factor to numeric?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM