简体   繁体   English

mutate_if() 多个条件

[英]mutate_if() multiple conditions

mpg |>
  dplyr::mutate_if(is.character, as.factor) |>
  dplyr::mutate_if(is.logical, as.factor) |>
  dplyr::mutate_if(is.integer, as.numeric)

I have the above code.我有上面的代码。 Is there a more simple way to write this code without calling mutate_if three times?有没有更简单的方法来编写这段代码而无需调用mutate_if三次?

If you check the documentation of mutate_if , it's been superseded by across() .如果您查看mutate_if文档,它已被 cross across()取代。

So the above code could be written as:所以上面的代码可以写成:

library(dplyr)

mpg %>% 
  mutate(across(where(is.character) | where(is.logical), as.factor),
         across(where(is.integer), as.numeric))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM