我想在 R 函式中輸入多個變數,并且我想在table1()函式中全部輸入它們==> 類似這一行的內容tab<-table1(~ var1 var2 var3 ... varN|group, data=data)
library(table1)
dataset<-data.frame(ID=c(1,1,2,2,3,3,4,4),group=c("gp1","gp2","gp1","gp2","gp1","gp2","gp1","gp2"),
col1=c(0,1,1,1,0,1,1,0),col2=c(0,0,1,1,1,1,0,0),col3=c(1,0,1,0,1,1,1,0))
print.f <- function(data,var1,...,group){
tab<-table1(~ var1 ...|group, data=data)
tab
}
print.f(data,var1,var2,var3,group=group)
print.f(dataset,col1,col2,col3)

例如,如果我有一個包含 3 列以上的資料集,并且我想查看它們的輸出,我該如何輸入所有這些資料?
uj5u.com熱心網友回復:
將公式創建為字符向量,轉換為公式類并運行 table1。在示例中,我們展示了使用 print.f 或直接使用 table1 創建相同輸出的幾種方法。
print.f <- function(data, ..., group) {
v <- paste(c(...), collapse = " ")
if (!missing(group)) v <- paste(v, "|", group)
fo <- as.formula(paste("~", v))
table1(fo, data = data)
}
library(table1)
print.f(dataset, "col1", "col2", "col3", group = "group")
print.f(dataset, c("col1", "col2", "col3"), group = "group")
print.f(dataset, grep("col", names(dataset), value = TRUE), group = "group")
print.f(dataset, names(dataset)[2:4], group = "group")
print.f(dataset[-1], ".", group = "group")
print.f(dataset, ". - ID", group = "group")
table1(~ . | group, dataset[-1])
table1(~ . - ID | group, dataset)
uj5u.com熱心網友回復:
這是一個操作語言而不是字串的解決方案。您和其他人op_literal()將來也可能會覺得有用。
解決方案
幫手: op_literal()
這個輔助函式op_literal()實際上操縱 R 語言本身來重復使用一個二元運算子,就像 跨多個運算元一樣......即使二元運算子通常只接受兩個運算元。呼叫op_literal(` `, w, x, y, z)實際上會產生這樣expression的位置:w x y z。
# Helper function to arbitrarily repeat a binary operation (like ' ').
op_literal <- function(op, ...) {
# Capture the operator as a symbol.
op_sym <- rlang::ensym(op)
# Count the operands.
n_dots <- rlang::dots_n(...)
# Recursive case: a binary operator cannot handle this many arguments.
if(n_dots > 2) {
# Split off the final operand.
dots <- rlang::exprs(...)
dots_last <- dots[[n_dots]]
dots <- dots[-n_dots]
# Perform recursion for the remaining operands.
op_left <- rlang::inject(op_literal(
op = !!op_sym,
... = !!!dots
))
# Assemble recursive results into the full operation.
substitute(op(op_left, dots_last))
}
# Base case: the binary operator can handle 2(-) arguments.
else {
substitute(op(...))
}
}
筆記
由于op_literal()生成了expression,eval如果您想要結果,您仍然需要對其進行評估:
op_exp <- op_literal(` `, 1, 2, 3, 4)
op_exp
#> 1 2 3 4
eval(op_exp)
#> [1] 10
自定義功能: print.f()
接下來,這個自定義print.f()然后利用op_literal()來組裝公式:
# Your custom 'print.f()' function.
print.f <- function(data, var1, ..., group) {
# Capture the core variables as symbols.
group_var <- rlang::ensym(group)
other_vars <- rlang::ensym(var1)
# Count the additional variables.
n_dots <- rlang::dots_n(...)
# Append those other variables if they exist.
if(n_dots > 0) {
other_vars <- rlang::inject(op_literal(op = ` `, !!other_vars, ...))
}
# Assemble the formula.
formula_exp <- rlang::inject(~ !!other_vars | !!group_var)
# Generate the table according to that formula.
table1::table1(
formula_exp,
data = data
)
}
結果
鑒于您dataset在此處轉載
dataset <- data.frame(
ID = c(1, 1, 2, 2, 3, 3, 4, 4),
group = c("gp1", "gp2", "gp1", "gp2", "gp1", "gp2", "gp1", "gp2"),
col1 = c(0, 1, 1, 1, 0, 1, 1, 0),
col2 = c(0, 0, 1, 1, 1, 1, 0, 0),
col3 = c(1, 0, 1, 0, 1, 1, 1, 0)
)
你的電話 print.f()
print.f(dataset, col1, col2, col3, group = group)
應該產生以下可視化:

筆記
就目前而言,您已經group在函式標頭的末尾定義了引數。這意味著,如果你嘗試呼叫print.f()像這樣
print.f(data = dataset, var = col1, col2, col3, group)
那么你會得到一個錯誤:如果沒有group = 規范,最終變數會與col2and混為一談col3,所有這些都在...保護傘下。這將產生一個錯誤的公式:
~ col1 col2 col3 group |
為了避免group = 每次都必須輸入的痛苦,您可以簡單地將其重新定位在 之前...,如下所示:
print.f <- function(data, group, var1, ...) {
# ^^^^^
完成后,以下呼叫將按您的預期作業:
print.f(dataset, group, col1, col2, col3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376153.html
上一篇:無論如何,這2個幾乎相似的函式是否可以壓縮為1個通用函式?
下一篇:如何在C 陣列中輸入亂數?
