r mutate_each function is deprecated

点点圈 提交于 2020-01-04 02:03:12

问题


I use the function mutate_each from the tidyverse package and I get a message this function is deprecated. I would like to use the other functions that are not deprecated to change field types.

Below is a reproducible example of how I currently employ mutate_each.

library(tidyverse)

set.seed(123)

df <- data.frame(FirstName = sample(LETTERS[1:2],size=3, replace=TRUE),
             LastName = sample(LETTERS[3:6],size=3, replace=TRUE),
             StartDate =  c("1/31/2000","2/1/2000","3/1/2000"),
             EndDate =   c("1/31/2010","2/10/2011","3/1/2016"),
             stringsAsFactors = FALSE)
str(df)

df %>% mutate_each(funs(as.factor(as.character(.))), 
               c(FirstName:LastName)) %>% 
   mutate_each(funs(as.Date(., format = "%m/%d/%Y",
                       origin = "1899-12-30")), 
          c(StartDate:EndDate))

`mutate_each()` is deprecated.
Use `mutate_all()`, `mutate_at()` or `mutate_if()` instead.
To map `funs` over a selection of variables, use `mutate_at()`...etc

I have played with mutate_all(), mutate_at() and mutate_if(), but no luck.


回答1:


Using the comments from @Chi Pak, the function mutate_at can be used to replace function mutate_each

library(tidyverse)

set.seed(123)

df <- data.frame(FirstName = sample(LETTERS[1:2],size=3, replace=TRUE),
         LastName = sample(LETTERS[3:6],size=3, replace=TRUE),
         StartDate =  c("1/31/2000","2/1/2000","3/1/2000"),
         EndDate =   c("1/31/2010","2/10/2011","3/1/2016"),
         stringsAsFactors = FALSE)

t1 <- df %>% mutate_each(funs(as.factor(as.character(.))), 
                     c(FirstName:LastName )) %>% 
  mutate_each(funs(as.Date(., format = "%m/%d/%Y",
                       origin = "1899-12-30")), 
          c(StartDate:EndDate))

t2 <- df %>% mutate_at(vars(FirstName:LastName),
                   funs(as.factor(as.character(.)))) %>% 
  mutate_at(vars(StartDate:EndDate),
        funs(as.Date(as.character(.),
                     format = "%m/%d/%Y", origin = "1899-12-30")))

identical(t1,t2)
[1] TRUE


来源:https://stackoverflow.com/questions/45090654/r-mutate-each-function-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!