df <- data.frame(A = c(1,1,1), B = c(2,2,3))
variables <- c("A", "B")
dplyr::count(df, !!!rlang::sym(variables))
Error: Only strings can be converted to symbols
Run `rlang::last_error()` to see where the error occurred.
如果variables是A或 則有效B,但不是兩者都有效。
uj5u.com熱心網友回復:
因為這syms是需要的。根據?sym
sym() 從字串創建一個符號, syms() 從字符向量創建一個符號串列。
dplyr::count(df, !!!rlang::syms(variables))
A B n
1 1 2 2
2 1 3 1
uj5u.com熱心網友回復:
有幾種方法可以做到這一點。OPs 方法的問題是sym()將回傳單個符號,但我們需要一個符號串列。使用 akrun 的優秀答案,使用矢量化syms或purrr::map(sym).
或者,沒有 rlang、雙/三劉海等,我們可以在 dplyr 中使用 across(all_of())
Library(tidyverse)
count(df, !!!syms(variables)) #As suggested by @akrun
count(df, across(all_of(variables)))
count(df, !!!map(variables, sym))
A B n
1 1 2 2
2 1 3 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/391169.html
上一篇:如何在不使用i和j的所有組合的情況下在列范圍上使用for回圈進行繪圖?
下一篇:前綴!!!包名,rlang
