Changing column types with dplyr

2020-07-27 04:10发布

问题:

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)

回答1:

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))

And the proof:

> 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 ...


回答2:

Honestly, I'd do it like this:

library(dplyr)

df = data.frame("LOC_ID" = c(1,2,3,4),
                "STRS" = c("a","b","c","d"),
                "UPC_CDE" = c(813,814,815,816))

df$LOC_ID = as.factor(df$LOC_ID)
df$UPC_CDE = as.factor(df$UPC_CDE)


标签: r dplyr mutate