使用dplyr更改列类型 [英] Changing column types with dplyr

查看:89
本文介绍了使用dplyr更改列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些整理数据的帮助。我正在尝试将一些整数转换为因数(但不是将所有整数转换为因数)。我认为我可以选择有问题的变量,但是如何将它们添加回原始数据集中呢?例如,保持未从raw_data_tbl中选择的值并使用 raw_data_tbl_int

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 but how do I add them back to the original data set? For example, keeping the values NOT selected from my raw_data_tbl and using the mutated types from the raw_data_tbl_int

    library(dplyr)

    raw_data_tbl %>% 
    select_if(is.numeric) %>% 
    select(-c(contains("units"), PRO_ALLOW, RTL_ACTUAL, REAL_PRICE, 
           REAL_PRICE_HHU, REBATE, RETURN_UNITS, UNITS_PER_CASE, Profit, STR_COST, DCC, 
           CREDIT_AMT)) %>% 
    mutate_if(is.numeric, as.factor)


推荐答案

您可以改用 mutate_at 。以下是使用 iris 数据框的示例:

You can use mutate_at instead. Here's an example using the iris dataframe:

library(dplyr)

iris_factor <- iris %>%
  mutate_at(vars(Sepal.Width, 
                 Sepal.Length), 
            funs(factor))




编辑08/2020


从dplyr 0.8.0开始, funs()已弃用。使用 list()代替,如


Edit 08/2020

As of dplyr 0.8.0, funs() is deprecated. Use list() instead, as in

library(dplyr)

iris_factor <- iris %>%
  mutate_at(vars(Sepal.Width, 
                 Sepal.Length), 
            list(factor))

和证明:

> str(iris_factor)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: Factor w/ 35 levels "4.3","4.4","4.5",..: 9 7 5 4 8 12 4 8 2 7 ...
 $ Sepal.Width : Factor w/ 23 levels "2","2.2","2.3",..: 15 10 12 11 16 19 14 14 9 11 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

这篇关于使用dplyr更改列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆