[英]Understand the warning message in across in R
This question is to build deeper understanding of R function Across & Which.这个问题是为了加深对 R function Across & Which 的理解。 I ran this code & got the message.
我运行了这段代码并得到了消息。 I want to understand
我想了解
a) what is the difference between good & bad pratice a) 好的和坏的做法有什么区别
b) How does where function work exactly in general & in this use case b)在哪里 function 在一般情况下和在这个用例中如何工作
library(tidyverse)
iris %>% mutate(across(is.character,as.factor)) %>% str()
Warning message:
Problem with `mutate()` input `..1`.
i Predicate functions must be wrapped in `where()`.
# Bad
data %>% select(is.character)
# Good
data %>% select(where(is.character))
i Please update your code.
There is not much difference between using where
and not using it.使用
where
和不使用 where 没有太大区别。 It just shows a warning to suggest a better syntax.它只是显示一个警告以建议更好的语法。 Basically
where
takes a predicate function and apply it on every variable (column) of your data set.基本上
where
需要一个谓词 function 并将其应用于数据集的每个变量(列)。 It then returns every variable for which the function returns TRUE
.然后它返回 function 为其返回
TRUE
的每个变量。 The following examples are taken from the documentations of where
:以下示例取自
where
的文档:
iris %>% select(where(is.numeric))
# or an anonymous function
iris %>% select(where(function(x) is.numeric(x)))
# or a purrr style formula as a shortcut for creating a function on the spot
iris %>% select(where(~ is.numeric(.x)))
Or you can also have two conditions using shorthand &&
:或者,您也可以使用简写
&&
有两个条件:
# The following code selects are numeric variables whose means are greater thatn 3.5
iris %>% select(where(~ is.numeric(.x) && mean(.x) > 3.5))
You can use select(where(is.character))
for .cols
argument of the across
function and then apply a function in .fns
argument on the selected columns.您可以将
select(where(is.character))
用于across
function 的.cols
参数,然后在所选列的.fns
参数中应用 function 。 For more information you can always refer to documentations which are the best source to learn more about these materials.有关更多信息,您始终可以参考文档,这是了解有关这些材料的更多信息的最佳来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.