本文介绍了r不推荐使用mutate_each函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了tidyverse包中的mutate_each函数,并且收到一条消息,提示该函数已被弃用.我想使用不推荐使用的其他功能来更改字段类型.
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.
下面是我目前如何使用mutate_each的可复制示例.
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
我玩过mutate_all(),mutate_at()和mutate_if(),但是没有运气.
I have played with mutate_all(), mutate_at() and mutate_if(), but no luck.
推荐答案
使用@Chi Pak的注释,函数mutate_at可用于替换函数mutate_each
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
这篇关于r不推荐使用mutate_each函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!