Rのこと。

記事は引っ越し作業中。2023年中までに引っ越しを完了させてブログは削除予定

tidyselectについてのメモ

カラムの選択でtidyselectパッケージのvars_select()を使った例。

library(tidyverse)
library(tidyselect)

n = 5
data <- tibble(
  y = rnorm(n, 100, 10),
  a1 = rnorm(n, 100, 10),
  b2 = rnorm(n, 100, 10),
  c3 = rnorm(n, 100, 10),
  a2 = rnorm(n, 100, 10),
  b3 = rnorm(n, 100, 10),
  a5 = rnorm(n, 100, 10),
  a3 = rnorm(n, 100, 10),
  a4 = rnorm(n, 100, 10),
  s = letters[1:n]
)

f <- function(df, ...){
  selected_cols <- tidyselect::vars_select(names(df), !!! rlang::quos(...))
  
  return(selected_cols)
}


data %>% f(df = ., y:s)
  y   a1   b2   c3   a2   b3   a5   a3   a4    s 
"y" "a1" "b2" "c3" "a2" "b3" "a5" "a3" "a4"  "s" 

data %>% f(df = ., starts_with("a"))
  a1   a2   a5   a3   a4 
"a1" "a2" "a5" "a3" "a4" 

data %>% f(df = ., ends_with("2"))
  b2   a2 
"b2" "a2" 

data %>% f(df = ., contains("3"))
  c3   b3   a3 
"c3" "b3" "a3" 

data %>% f(df = ., num_range(prefix = "a", range = 3:4))
  a3   a4 
"a3" "a4" 

メモしたもののvars_select()は今後、eval_select()が推奨されるとのこと。