[英]Understand the warning message in across in R
這個問題是為了加深對 R function Across & Which 的理解。 我運行了這段代碼並得到了消息。 我想了解
a) 好的和壞的做法有什么區別
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.
使用where
和不使用 where 沒有太大區別。 它只是顯示一個警告以建議更好的語法。 基本上where
需要一個謂詞 function 並將其應用於數據集的每個變量(列)。 然后它返回 function 為其返回TRUE
的每個變量。 以下示例取自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)))
或者,您也可以使用簡寫&&
有兩個條件:
# The following code selects are numeric variables whose means are greater thatn 3.5
iris %>% select(where(~ is.numeric(.x) && mean(.x) > 3.5))
您可以將select(where(is.character))
用於across
function 的.cols
參數,然后在所選列的.fns
參數中應用 function 。 有關更多信息,您始終可以參考文檔,這是了解有關這些材料的更多信息的最佳來源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.