R語言 dplyr selec 輔助函數

Tidyverse selections implement a dialect of R where operators make it easy to select variables:

: for selecting a range of consecutive variables.

! for taking the complement of a set of variables.

& and | for selecting the intersection or the union of two sets of variables.

c() for combining selections.

In addition, you can use selection helpers such as:

everything(): Matches all variables.

last_col(): Select last variable, possibly with an offset.

These helpers select variables based on their names:

starts_with(): Starts with a prefix.

ends_with(): Ends with a suffix.

contains(): Contains a literal string.

matches(): Matches a regular expression.

num_range(): Matches a numerical range like x01, x02, x03.

These functions select variables from a character vector.

all_of(): Matches variable names in a character vector. All names must be present, otherwise an out-of-bounds error is thrown.

any_of(): Same as all_of(), except that no error is thrown for names that don’t exist.

select使用變量作爲參數的時候會警告,select(var_vector)的正確寫法應該是select(all_of(var_vector)),因爲可能出現 var_vector這個變量名本身是column name的情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章