gpt4 book ai didi

r mutate_each 函数已弃用

转载 作者:行者123 更新时间:2023-12-04 11:50:32 25 4
gpt4 key购买 nike

我使用 tidyverse 包中的函数 mutate_each,但收到一条消息,该函数已被弃用。我想使用其他未弃用的函数来更改字段类型。

下面是我目前如何使用 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(),但运气不好。

最佳答案

使用@Chi Pak 的评论,函数 mutate_at 可以用来替换函数 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 函数已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45090654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com